简体   繁体   中英

Asking for better solution for creating a new column that contains specific word in existing columns

I have the following data frame.

data = pd.DataFrame()
data ['id1_des'] = ['Accurate','Through','Accurate', 'Blocked']
data ['id2_des'] = ['','Foot','', 'Not Accurate']
data ['id3_des'] = ['','shot','', '']
data ['id4_des'] = ['','Accurate','', '']

I am trying to create a new column that contains Accurate or Not Accurate from existing 4 columns.

I used the following method:

Con1 = 'Accurate'
data['accuracy'] = np.select([Con1 ==data.id1_des,Con1 ==data.id2_des,Con1 ==data.id3_des,Con1 ==data.id4_des],['Accurate','Accurate','Accurate','Accurate'],default = 'Not Accurate')

I got what I wanted to create. However, I would like to ask if anyone can advise better solution for this?

My output is as follow:

在此处输入图片说明

Thanks, Thanks,

Zep

Using ffill

data['accuracy']=data.replace('',np.nan).ffill(axis = 1).iloc[:,-1]
data
Out[23]: 
    id1_des       id2_des id3_des   id4_des      accuracy
0  Accurate                                      Accurate
1   Through          Foot    shot  Accurate      Accurate
2  Accurate                                      Accurate
3   Blocked  Not Accurate                    Not Accurate

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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