简体   繁体   中英

Create a new derived column in pandas if value inside the column is non-null

My input data like this

SL.NO   Name
1      KING  BATA
2   
3   
4     AGS
5     FORMULA GROWTH 
6   
7     Bag

Output

SL.NO   Name               Value
1     KING  BATA          Present
2                         Not Present
3                         Not Present
4   AGS                   Present
5   FORMULA GROWTH       Present
6                        Not Present
7   Bag                  Present

How to handle null, blank and junk values in pandas?

Use numpy.where :

#If missing value is NaN
df['Value'] = np.where(df['Name'].isnull(), 'Present', 'Not Present')

Or:

#If missing value is empty string
df['Value'] = np.where(df['Name'].eq(''), 'Present', 'Not Present')

Fun with pd.Categorical :

df

   SL.NO            Name
0      1       KING BATA
1      2                
2      3                
3      4             AGS
4      5  FORMULA GROWTH
5      6                
6      7             Bag

df['Value'] = pd.Categorical.from_codes(df.Name.astype(bool),
                              categories=['Not Present', 'Present'])
df

   SL.NO            Name        Value
0      1       KING BATA      Present
1      2                  Not Present
2      3                  Not Present
3      4             AGS      Present
4      5  FORMULA GROWTH      Present
5      6                  Not Present
6      7             Bag      Present

Which, incidentally, works regardless of whether your missing values are NaN s, None , or '' , because astype(bool) takes advantage of the false-yness of these values:

df

   SL.NO            Name        Value
0      1       KING BATA      Present
1      2            None  Not Present
2      3            None  Not Present
3      4             AGS      Present
4      5  FORMULA GROWTH      Present
5      6            None  Not Present
6      7             Bag      Present

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