简体   繁体   English

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

[英]Pandas Replace NaN with blank/empty string

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
df = df.fillna('')

or just要不就

df.fillna('', inplace=True)

This will fill na's (eg NaN's) with '' .这将用''填充 na(例如 NaN)。

If you want to fill a single column, you can use:如果要填充单个列,可以使用:

df.column1 = df.column1.fillna('')

One can use df['column1'] instead of df.column1 .可以使用df['column1']而不是df.column1

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

This might help.这可能会有所帮助。 It will replace all NaNs with an empty string.它将用空字符串替换所有 NaN。

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)

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('')

Use a formatter, if you only want to format it so that it renders nicely when printed .使用格式化程序,如果您只想对其进行格式化,以便在打印时呈现良好的效果 Just use the df.to_string(... formatters to 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

Try this,尝试这个,

add inplace=True添加inplace=True

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

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

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

If you are converting DataFrame to JSON, NaN will give error so best solution is in this use case is to replace NaN with None .如果您将 DataFrame 转换为 JSON, NaN会出错,因此在此用例中最好的解决方案是将NaN替换为None
Here is how:方法如下:

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

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)

To remove the nan and fill some values:要删除 nan 并填充一些值:

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

I tried df.iloc also.我也试过 df.iloc。 but it needs the index of the column.但它需要列的索引。 so you need to look into the table again.所以你需要再次查看表格。 simply the above method reduced one step.简单地把上面的方法减少了一个步骤。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM