[英]Use None instead of np.nan for null values in pandas DataFrame
[英]Pandas dataframe compare values == none / nothing / null
假設我在 pandas dataframe 中有 2 列。
我想檢查Column A
列中的每一row
是否有任何NOT == NaN
的值。
如果找到一個值,則row
對應的行為'P'
IF NaN
value then 'B'
在 excel 中,我可以使用=if(A1="","B","P"
假設單元格為empty
而不是NAN
我認為我的 excel 背景讓我對什么被認為是empty
值或null
與什么是NaN
值感到困惑。
<<Test Frame>>
Column A | Column B
1 NaN NaN
2 John NaN
3 Dave NaN
4 NaN NaN
5 Michael NaN
<<Desired Output>>
Column A | Column B
1 NaN B
2 John P
3 Dave P
4 NaN B
5 Michael P
我已經對 SO 進行了研究,但找不到適合這個特定目的的方法。
在 pandas 中我們有np.where
import numpy as np
df['colB'] = np.where(df['colA'].isna(), 'B', 'P')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.