
[英]How can I replace NaN value with mean in a Pandas dataframe?
我有一个数据框来填充NaN值,如下所示: 我打算根据其类别将以下NaN值替换为上述Rating。 例如,由于约会类别的评分为4.0,因此应用A应填写4.0。 ...
[英]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.