Multiple logical comparisons in pandas df

。_饼干妹妹 提交于 2021-02-02 09:28:28

问题


If I have the following pandas df

A   B   C   D
1   2   3   4
2   2   3   4

and I want to add a new column to be 1, 2 or 3 depending on,

(A > B) && (B > C) = 1
(A < B) && (B < C) = 2
Else = 3

whats the best way to do this?


回答1:


You can use numpy.select to structure your multiple conditions. The final parameter represents default value.

conditions = [(df.A > df.B) & (df.B > df.C),
              (df.A < df.B) & (df.B < df.C)]

values = [1, 2]

df['E'] = np.select(conditions, values, 3)

There are several alternatives: nested numpy.where, sequential pd.DataFrame.loc, pd.DataFrame.apply. The main benefit of this solution is readability while remaining vectorised.




回答2:


you can use apply on df with your two conditions such as:

df['E'] = df.apply(lambda x: 1 if x.A > x.B and x.B > x.C else 2 if x.A < x.B and x.B < x.C else 3, axis=1)



回答3:


This can also be solved using indexing and fillna.

df.loc[(df['A'] > df['B'])
  &(df['B'] > df['C']), 'New_Col'] = 1

df.loc[(df['A'] < df['B'])
  &(df['B'] < df['C']), 'New_Col'] = 2

df['New_Col'] = df['New_Col'].fillna(3)

The first chunk of code is read like so: locate where A > B and B > C, if both of these conditions are true, set the column 'New_Col' equal to 1. The second chunk can be interpreted in the same way. If both the first and second chunk do no return a 1 or 2, then they will appear as null. Use the fillna() function to fill those nulls with a 3.

This will produce the following dataframe:



来源:https://stackoverflow.com/questions/50140131/multiple-logical-comparisons-in-pandas-df

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!