简体   繁体   English

如何替换“对象”列中的 nan 值?

[英]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
问题未解决?试试搜索: 如何替换“对象”列中的 nan 值?
暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在熊猫数据框中用均值替换NaN值? - How can I replace NaN value with mean in a Pandas dataframe? 如何用列中的日期替换 nan 值? - How to replace nan value with a date in a column? 熊猫-如何在NaN列中实现每个值? - Pandas - How can I make every value within a column NaN? 如何计算列值等于NaN或零? - How can I count if column value is equal to NaN or zero? 如何使用替换功能在数据帧中用 NAN 替换 XNA 值? - How i can replace the XNA value with NAN in dataframe using Replace function? 如何用不在熊猫系列中先前值某个范围内的NAN替换任何值? - How can I replace any value with an NAN that is not within a certain range of the previous value in a pandas series? 使用 Python Pandas,仅当“nan”值不存在时,我可以根据另一列替换 df 中一列的值吗? - Using Python Pandas, can I replace values of one column in a df based on another column only when a "nan" value does not exist? 如何替换熊猫数据框中列中的 NaN 值? - how to replace a NaN value in a column in data frame in pandas? 如果旁边列上的值不是 NaN,则用 NaN 替换列值 - Replace column values with NaN if the value on the column next to it is not NaN 如果列标题中的值,如何替换值? - How can I replace a value if the value in the column title?
 
粤ICP备18138465号  © 2020-2023 STACKOOM.COM