pandas 如何用我的 DataFrame 中的空字符串替换所有“nan”字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38922952/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Replace All the "nan" Strings with Empty String in My DataFrame?
提问by MEhsan
I have "None"
and "nan"
strings scattered in my dataframe. Is there a way to replace all of those with empty string ""
or nan
so they do not show up when I export the dataframe as excel sheet?
我"None"
和"nan"
字符串散落在我的数据帧。有没有办法用空字符串替换所有这些,""
或者nan
当我将数据框导出为 Excel 表时它们不显示?
Simplified Example:
简化示例:
Note:nan
in col4
are not strings
注意:nan
incol4
不是字符串
ID col1 col2 col3 col4
1 Apple nan nan nan
2 None orange None nan
3 None nan banana nan
The output should be like this after removing all the "None"
and "nan"
strings when we replaced them by empty strings ""
:
当我们用空字符串替换它们时,删除所有"None"
和"nan"
字符串后的输出应该是这样的""
:
ID col1 col2 col3 col4
1 Apple nan
2 orange nan
3 banana nan
Any idea how to solve this problem?
知道如何解决这个问题吗?
Thanks,
谢谢,
回答by Jon Clements
Use a list of strings to replace with blanks strings that won't affect actual nan
's...
使用字符串列表替换不会影响实际nan
的空白字符串...
df.replace(['nan', 'None'], '')
Which'll give you a new dataframe of:
这会给你一个新的数据框:
ID col1 col2 col3 col4
1 Apple NaN
2 orange NaN
3 banana NaN
回答by Alexander
You can compare each column to the numpy object dtype (which is basically text columns), and then only do replacements for those columns.
您可以将每一列与 numpy 对象 dtype(基本上是文本列)进行比较,然后只对这些列进行替换。
for col in df:
if df[col] == np.dtype('O'): # Object
df.col1.replace(['None', 'NaN', np.nan], "", inplace=True)
回答by ayhan
回答by Kartik
All those loop-de-loop solutions...
所有这些循环去循环的解决方案......
replacers = [None, np.nan, "None", "NaN", "nan"] # and everything else that needs replacing.
df.loc[:, df.dtypes == 'object'].replace(replacers, '', inplace=True)