python - 将 lambda 与 pandas 结合使用以现有列为条件计算新列

标签 python pandas dataframe lambda

我需要在 pandas DataFrame 中创建一个新列,其计算方式为 DataFrame 中 2 个现有列的比率。但是,比率计算中的分母将根据 DataFrame 中另一列中找到的字符串值而变化。

示例。示例数据集:

import pandas as pd
df = pd.DataFrame(data={'hand'      : ['left','left','both','both'], 
                        'exp_force' : [25,28,82,84], 
                        'left_max'  : [38,38,38,38], 
                        'both_max'  : [90,90,90,90]})

我需要根据df['hand']的条件创建一个新的DataFrame列df['ratio']

如果df['hand']=='left'df['ratio'] = df['exp_force']/df['left_max']

如果df['hand']=='both'df['ratio'] = df['exp_force']/df['both_max']

最佳答案

您可以使用np.where():

import pandas as pd
df = pd.DataFrame(data={'hand'      : ['left','left','both','both'], 
                        'exp_force' : [25,28,82,84], 
                        'left_max'  : [38,38,38,38], 
                        'both_max'  : [90,90,90,90]})
df['ratio'] = np.where((df['hand']=='left'), df['exp_force'] / df['left_max'], df['exp_force'] / df['both_max'])
df

Out[42]: 
   hand  exp_force  left_max  both_max     ratio
0  left         25        38        90  0.657895
1  left         28        38        90  0.736842
2  both         82        38        90  0.911111
3  both         84        38        90  0.933333

或者,在现实生活中,如果您有很多条件和结果,那么您可以使用np.select(),这样您就不必不断重复np.where() 语句,因为我在旧代码中做了很多工作。在这些情况下最好使用np.select:

import pandas as pd
df = pd.DataFrame(data={'hand'      : ['left','left','both','both'], 
                        'exp_force' : [25,28,82,84], 
                        'left_max'  : [38,38,38,38], 
                        'both_max'  : [90,90,90,90]})
c1 = (df['hand']=='left')
c2 = (df['hand']=='both')
r1 = df['exp_force'] / df['left_max']
r2 = df['exp_force'] / df['both_max']
conditions = [c1,c2]
results = [r1,r2]
df['ratio'] = np.select(conditions,results)
df
Out[430]: 
   hand  exp_force  left_max  both_max     ratio
0  left         25        38        90  0.657895
1  left         28        38        90  0.736842
2  both         82        38        90  0.911111
3  both         84        38        90  0.933333

关于python - 将 lambda 与 pandas 结合使用以现有列为条件计算新列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63534380/

相关文章:

r - 根据列值复制数据框中的行

python - 如何 pickle 一个写在函数内部的类的实例?

python - 存在重复项时删除另一个数据框中的行

python - 将数据框的特定列乘以常量标量值

python - 在 pandas 中绘制部分堆积条形图

python - 如何使用 Pandas Dataframe 重复滚动减法

javascript - 删除 HTML 文件中的任何字符串 python

python - 无法启动Tor浏览器

python - 仅当存在公共(public)索引时如何组合两个数据帧,否则保留空单元格

python - 通过检查 pandas 数据框来替换单词