[英]How can I replace a nan value in an "object" column?
提示:本站為國內最大中英文翻譯問答網站,提供中英文對照查看,鼠標放在中文字句上可顯示英文原文。
如何替換作為“對象”的列中的 nan 值,包含其他 str 並確保它進入主 df 而不僅僅是切片。
我試過這個
covid_19_df.Country_code.fillna('OT')
但它仍然是空的
D Country_code Country
1. OM Oman
2. OM Oman
3. Other
4. Other
5. Other
我希望它看起來像這樣
D Country_code Country
1. OM Oman
2. OM Oman
3. OT Other
4. OT Other
5. OT Other
fillna
默認不替換inplace,必須保存操作:
covid_19_df.Country_code = covid_19_df.Country_code.fillna('OT')
如果您有空字符串而不是NaN
,則可以replace
它們替換為pd.NA
:
covid_19_df.Country_code = covid_19_df.Country_code.replace('', pd.NA).fillna('OT')
Output:
>>> covid_19_df
Country_code Country
0 OM Oman
1 OM Oman
2 OT Other
3 OT Other
4 OT Other
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.