我需要在 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/