[英]How can I replace a nan value in an "object" column?
提示:本站为国内最大中英文翻译问答网站,提供中英文对照查看,鼠标放在中文字句上可显示英文原文。
How can I replace a nan value in a column which is an 'object', contains other str and ensure it goes into the main df and not just the slice.如何替换作为“对象”的列中的 nan 值,包含其他 str 并确保它进入主 df 而不仅仅是切片。
I have tried this我试过这个
covid_19_df.Country_code.fillna('OT')
but it is still empty但它仍然是空的
D Country_code Country
1. OM Oman
2. OM Oman
3. Other
4. Other
5. Other
I want it to look like this我希望它看起来像这样
D Country_code Country
1. OM Oman
2. OM Oman
3. OT Other
4. OT Other
5. OT Other
fillna
does not replace inplace by default, you have to save the operation: fillna
默认不替换inplace,必须保存操作:
covid_19_df.Country_code = covid_19_df.Country_code.fillna('OT')
If you have empty strings instead NaN
, you can replace
them by pd.NA
:如果您有空字符串而不是
NaN
,则可以replace
它们替换为pd.NA
:
covid_19_df.Country_code = covid_19_df.Country_code.replace('', pd.NA).fillna('OT')
Output: 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.