在新列中过滤结果

时间:2019-12-09 20:26:01

标签: pandas filter

我的熊猫文件结构如下(python 3)

Particulars   AMT
AA(AED) 
QP             7
WP             8
ST             9
AB(USD) 
RR             6
RA             8
DA(INR) 
DRS            5
DRW            3
UTS            6

我希望输出如下(python 3)

Particulars     AMT      LOG
AA(AED)                 AA(AED)
QP               7  
WP               8  
ST               9  
AB(USD)                 AB(USD)
RR               6  
RT               8  
DA(INR)                 DA(INR)
DRS              5  
DRW              3  
UTS              6  

2 个答案:

答案 0 :(得分:1)

尝试一下:

df['LOG'] = None
df['LOG'] = df[df['Particulars'].str.contains(r'\w+\(\w+\)')].reindex(range(0,len(df))).fillna('') 

输出:

   Particulars AMT      LOG
0      AA(AED)      AA(AED)
1           QP   7         
2           WP   8         
3           ST   9         
4      AB(USD)      AB(USD)
5           RR   6         
6           RA   8         
7      DA(INR)      DA(INR)
8          DRS   5         
9          DRW   3         
10         UTS   6   

答案 1 :(得分:0)

temp=df.Particulars.fillna("0")
df['LOG'] = pd.np.where(temp.str.contains("0"),"",
                   pd.np.where(temp.str.contains(r'\(\w+\)'), df['Particulars'],''))
df