简体   繁体   English

根据条件进行数据帧过滤

[英]Data-frame filter according to a condition

I have a data-frame like 我有一个像数据框架

colA  colB  colC
 A      B    C 
 A      D    C 
 B      B    E 
 A      D    C 
 C      B    C 

I want to filter them in a priority like this: If colC == E then return E, after that check colB == D return D otherwise return colA The output is 我想以这样的优先级过滤它们:如果colC == E然后返回E,那么检查colB == D返回D否则返回colA输出是

colA  colB  colC   final
 A      B    C      A 
 A      D    C      D
 B      B    E      E 
 A      D    C      D
 C      B    C      C

Create the condition Series , the chain with bfill and fillna 创建条件Series ,带有bfillfillna

s=pd.Series({'colB':'D','colC':'E'})
df['New']=df.where(df.eq(s)).bfill(1).iloc[:,0].fillna(df.colA)

>>> df
  colA colB colC New
0    A    B    C   A
1    A    D    C   D
2    B    B    E   E
3    A    D    C   D
4    C    B    C   C

You could use np.select , which allows you to select among multiple values depending on a list of conditions: 您可以使用np.select ,它允许您根据条件列表在多个值中进行选择:

m1 = df.colC =='E'
m2 = df.colB =='D'
df.loc[:,'final'] = np.select([m1,m2], ['E', 'D'], default=df.colA)

   colA colB colC final
0    A    B    C     A
1    A    D    C     D
2    B    B    E     E
3    A    D    C     D
4    C    B    C     C

My favorite is to use a chained mask() , like this: 我最喜欢的是使用链式mask() ,如下所示:

df["final"] = df["colA"] \
              .mask(df["colB"].eq("D"), "D") \
              .mask(df["colC"].eq("E"), "E")

This is to present your if-then-elif sequence in exact reverse order of checking, but otherwise very readable. 这是以完全相反的检查顺序呈现if-then-elif序列,但在其他方面非常易读。

Don't take this seriously 不要认真对待这件事

I'm just experimenting 我只是在试验

a = df.colA.values.copy()                # Set lowest priority first
a[np.flatnonzero(df.colB == 'D')] = 'D'  # And on down the line
a[np.flatnonzero(df.colC == 'E')] = 'E'  # Highest priority last

df.assign(New=a)

  colA colB colC New
0    A    B    C   A
1    A    D    C   D
2    B    B    E   E
3    A    D    C   D
4    C    B    C   C

using np.where 使用np.where

t['final'] = np.where(t['colC'] == 'E', 'E', (np.where(t['colB'] == 'D', 'D', t['colA'])))

Output 产量

  colA colB colC final
0    A    B    C     A
1    A    D    C     D
2    B    B    E     E
3    A    D    C     D
4    C    B    C     C

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM