根据空白列为结果创建新列

时间:2019-12-11 05:25:02

标签: python pandas filter


I have table as below, in row number 2("AAPOP"), 6("AB"), 9("QDA98"), I don't have any value in "remarks" and "details" column


Particulars          Remark    Details         AMT  
AAPOP               
QP                       av      daf            7   
WP                       ad      dae            8   
ST                       df      dds            9   
AB              
RR                      dasd      df            6   
RA                       dsf      we            8   
QDA98               
DRS                      sd       df            5   
DRW                      sd       dsa           3   
UTS                     dad       fds           6   

I want to create "log" column and paste value for those column who is not contain any value in column 'remarks' and 'details', in below example it is "AAPOP, "AB", "QDA98"



Particulars         Remark    Details   log      AMT
AAPOP                                  AAPOP    
QP                     av       daf               7
WP                     ad       dae               8
ST                     df       dds               9
AB                                       AB 
RR                    dasd       df               6
RA                     dsf       we               8
QDA98                                   QDA98   
DRS                     sd       df               5
DRW                     sd      dsa               3
UTS                    dad      fds               6




3 个答案:

答案 0 :(得分:2)

IIUC,试试这个

df['log']= np.where((df.Remark.isnull()) & (df.Remark.isnull()),df.Particulars,'')
df

输入

Particulars     Remark  Details     AMT
0   AAPOP   None    None    NaN
1   QP  av  daf     7.0
2   WP  ad  dae     8.0
3   ST  df  dds     9.0
4   AB  None    None    NaN
5   RR  dasd    df  6.0
6   RA  dsf     we  8.0
7   QDA98   None    None    NaN
8   DRS     sd  df  5.0
9   DRW     sd  dsa     3.0
10  UTS     dad     fds     6.0

输出

Particulars     Remark  Details     AMT     log
0   AAPOP   None    None    NaN     AAPOP
1   QP  av  daf     7.0     
2   WP  ad  dae     8.0     
3   ST  df  dds     9.0     
4   AB  None    None    NaN     AB
5   RR  dasd    df  6.0     
6   RA  dsf     we  8.0     
7   QDA98   None    None    NaN     QDA98
8   DRS     sd  df  5.0     
9   DRW     sd  dsa     3.0     
10  UTS     dad     fds     6.0     

答案 1 :(得分:2)

您可能想尝试一下:

def log_func(row):
    # return the value in Particulars column if these columns are empty
    if not any([row['Remark'], row['Details'], row['AMT']])
        return row['Particulars']
    else 
        return ''

d['log'] = d.apply(log_func, axis=1)

答案 2 :(得分:1)

我不确定这是可行的,尽管我不确定100%。如果没有,解决方案应该非常相似。我明天将对其进行测试并更新我的帖子。

df['log'] = ''
df.loc[(df['Remark'] == '') & (df['Details'] == ''), 'log'] = df['Particulars']