我是python的新手,仍然在学习。我非常感谢您可以提供的任何帮助。我的数据框中有五列。我想将S1与S2进行比较,然后根据几个条件,添加一列,其中包含代表每个样本ID更改方式的字符串值。
Id S1 Cat_1 S2 Cat2
25 3 Acq 4 Acq
15 9 Chp 9 Chp
22 4 Acq 5 Frn
34 4 Acq 1 Acq
45 3 Acq 9 Chp
到目前为止,我已经这样做了,但是还不知道如何添加更多条件。
df['new column'] = np.where(df['S2']>df['S1'],'More','no')
What I would like is:
if ['S2']>['S1'] = 'More'
OR
if ['S2']<['S1'] = 'Less'
OR
if ['S2']=['S1'] = 'Same'
OR
if none of the above conditions are true, or if S1 is nan, new column value = 'New'
结果应如下所示:
Id S1 Cat_1 S2 Cat2 New_column
25 3 Acq 4 Acq More
15 9 Chp 9 Chp Same
22 4 Acq 5 Frn More
34 4 Acq 1 Acq Less
45 3 Acq 9 Chp More
01 9 Chp New
谢谢
答案 0 :(得分:1)
您可以在false参数的np.where()
内连接条件,也可以使用您的条件创建自定义函数,然后将apply
与lambda
一起传递,我将展示两个选项:< / p>
使用乘法np.where()
:
df['new column'] = np.where(df['S1'].isna(),'New',
np.where(df['S2']>df['S1'],'More',
np.where(df['S2'] < ['S1'],'Less','Same')))
创建自定义功能:
def my_function(x):
if x['S1'].isna():
return 'New'
elif x['S2'] > x['S1']:
return "More"
elif x['S2'] < x['S1']:
return "Less"
else:
return "Same"
df['New column'] = df.apply(lambda x: my_function(x),axis=1)