我有一个关于在Python中选择和应用函数的问题。
说我的DataFrame是:
Col 0 Col 1 Col 2 Col 3
Good 1 3 1
Good 2 2 1
Bad 0 1 1
我想创建另一个列'结果'基于Col 0'值:
1. Col 1 - Col 2, if Col 0 == 'Good'
2. Col 3 - Col 2, if Col 0 == 'Bad'
I.e:
For the first row, result = 1-3 = -2
For the third row, result = 1-1 = 0
如何实现这一目标?
非常感谢!!!!!!!
答案 0 :(得分:1)
您可以使用2次loc
来电:
In [46]:
df.loc[df['Col0'] == 'Good', 'Result'] = df['Col1'] - df['Col2']
df.loc[df['Col0'] == 'Bad', 'Result'] = df['Col3'] - df['Col2']
df
Out[46]:
Col0 Col1 Col2 Col3 Result
0 Good 1 3 1 -2
1 Good 2 2 1 0
2 Bad 0 1 1 0
或np.where
:
In [48]:
df['Result'] = np.where(df['Col0'] == 'Good', df['Col1'] - df['Col2'], df['Col3'] - df['Col2'])
df
Out[48]:
Col0 Col1 Col2 Col3 Result
0 Good 1 3 1 -2
1 Good 2 2 1 0
2 Bad 0 1 1 0
np.where
版本假定行不是“好”的行。是'坏''或者您想要执行df['Col3'] = df['Col2']
而不是