Python Pandas 用空白/空字符串替换 NaN

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26837998/
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-08-19 01:04:04  来源:igfitidea点击:

Pandas Replace NaN with blank/empty string

pythonpandasdataframenan

提问by user1452759

I have a Pandas Dataframe as shown below:

我有一个 Pandas 数据框,如下所示:

    1    2       3
 0  a  NaN    read
 1  b    l  unread
 2  c  NaN    read

I want to remove the NaN values with an empty string so that it looks like so:

我想用空字符串删除 NaN 值,使其看起来像这样:

    1    2       3
 0  a   ""    read
 1  b    l  unread
 2  c   ""    read

采纳答案by nEO

import numpy as np
df1 = df.replace(np.nan, '', regex=True)

This might help. It will replace all NaNs with an empty string.

这可能会有所帮助。它将用空字符串替换所有 NaN。

回答by fantabolous

df = df.fillna('')

or just

要不就

df.fillna('', inplace=True)

This will fill na's (e.g. NaN's) with ''.

这将用 .na 填充 na(例如 NaN)''

If you want to fill a single column, you can use:

如果要填充单个列,可以使用:

df.column1 = df.column1.fillna('') # this was wrong in a previous edit

回答by Natesh bhat

If you are reading the dataframe from a file (say CSV or Excel) then use :

如果您正在从文件(例如 CSV 或 Excel)中读取数据框,请使用:

  • df.read_csv(path , na_filter=False)
  • df.read_excel(path , na_filter=False)
  • df.read_csv(path , na_filter=False)
  • df.read_excel(path , na_filter=False)

This will automatically consider the empty fields as empty strings ''

这将自动将空字段视为空字符串 ''



If you already have the dataframe

如果您已经有了数据框

  • df = df.replace(np.nan, '', regex=True)
  • df = df.fillna('')
  • df = df.replace(np.nan, '', regex=True)
  • df = df.fillna('')

回答by Dinesh Khetarpal

If you are converting DataFrame to JSON, NaNwill give error so best solution is in this use case is to replace NaNwith None.
Here is how:

如果您将 DataFrame 转换为 JSON,NaN则会出错,因此在此用例中最好的解决方案是替换NaNNone.
方法如下:

df1 = df.where((pd.notnull(df)), None)

回答by Steve Schulist

Use a formatter, if you only want to format it so that it renders nicely when printed. Just use the df.to_string(... formattersto define custom string-formatting, without needlessly modifying your DataFrame or wasting memory:

使用格式化程序,如果您只想对其进行格式化,以便在打印时呈现良好的效果。只需使用df.to_string(... formatters来定义自定义字符串格式,而无需不必要地修改您的 DataFrame 或浪费内存:

df = pd.DataFrame({
    'A': ['a', 'b', 'c'],
    'B': [np.nan, 1, np.nan],
    'C': ['read', 'unread', 'read']})
print df.to_string(
    formatters={'B': lambda x: '' if pd.isnull(x) else '{:.0f}'.format(x)})

To get:

要得到:

   A B       C
0  a      read
1  b 1  unread
2  c      read

回答by Bendy Latortue

using keep_default_na=Falseshould help you:

使用keep_default_na=False应该可以帮助您:

df = pd.read_csv(filename, keep_default_na=False)

回答by Subbu VidyaSekar

I tried with one column of string values with nan.

我尝试使用 nan 使用一列字符串值。

To remove the nan and fill the empty string:

要删除 nan 并填充空字符串:

df.columnname.replace(np.nan,'',regex = True)

df.columnname.replace(np.nan,'',regex = True)

To remove the nan and fill some values:

要删除 nan 并填充一些值:

df.columnname.replace(np.nan,'value',regex = True)

df.columnname.replace(np.nan,'value',regex = True)

I tried df.iloc also. but it needs the index of the column. so you need to look into the table again. simply the above method reduced one step.

我也试过 df.iloc。但它需要列的索引。所以你需要再次查看表格。简单地把上面的方法减少了一个步骤。

回答by Vineesh TP

Try this,

尝试这个,

add inplace=True

添加 inplace=True

import numpy as np
df.replace(np.NaN, ' ', inplace=True)