簡體   English   中英

如果列中的值不為null,則在pandas中創建一個新的派生列

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

我的輸入數據是這樣的

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

產量

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

如何處理熊貓中的空值,空白值和垃圾值?

使用numpy.where

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

要么:

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

pd.Categorical一起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

順便說一句,無論您缺少的值是NaN s, None還是'' ,它都可以工作,因為astype(bool)利用了這些值的虛假性:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM