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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 01:48:34  来源:igfitidea点击:

How to Replace All the "nan" Strings with Empty String in My DataFrame?

pythonstringpandasdataframenan

提问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 nanso they do not show up when I export the dataframe as excel sheet?

"None""nan"字符串散落在我的数据帧。有没有办法用空字符串替换所有这些,""或者nan当我将数据框导出为 Excel 表时它们不显示?

Simplified Example:

简化示例:

Note:nanin col4are not strings

注意:nanincol4不是字符串

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

Use pandas' NaN. Those cells will be empty in Excel (you will be able to use 'select empty cells' command for example. You cannot do that with empty strings).

使用Pandas的 NaN。这些单元格在 Excel 中将是空的(例如,您将能够使用“选择空单元格”命令。您不能使用空字符串执行此操作)。

import numpy as np
df.replace(['None', 'nan'], np.nan, inplace=True)

enter image description here

在此处输入图片说明

enter image description here

在此处输入图片说明

回答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)