pandas 如何替换python中的NaN值

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

how to replace NaN value in python

pythonpandasdataframemissing-dataimputation

提问by TheRight

I have a list of NaN value into my dataframe.

我的数据框中有一个 NaN 值列表。

i want to replace NaN value with empty.

我想用空替换 NaN 值。

I try

我试试

        df_conbid_N_1 = pd.read_csv("test-2019.csv",dtype=str, sep=';', encoding='utf-8')
        df_conbid_N_1['Excep_Test'] = df_conbid_N_1['Excep_Test'].replace("NaN","")

But it's dosen't work

但它不起作用

Please help

请帮忙

回答by Hemil Patel

Simple! you can do this way

简单的!你可以这样做

df_conbid_N_1 = pd.read_csv("test-2019.csv",dtype=str, sep=';',encoding='utf-8').fillna("")

回答by meW

Use fillna: An example -

使用fillna:一个例子 -

df = pd.DataFrame({'no': [1, 2, 3],
                    'Col1':['State','City','Town'],
                  'Col2':['abc', np.NaN, 'defg'],
                  'Col3':['Madhya Pradesh', 'VBI', 'KJI']})

df

   no   Col1    Col2    Col3
0   1   State   abc Madhya Pradesh
1   2   City    NaN VBI
2   3   Town    defg    KJI

df.Col2.fillna('', inplace=True)
df

    no  Col1    Col2    Col3
0   1   State   abc     Madhya Pradesh
1   2   City            VBI
2   3   Town    defg    KJI

回答by yatu

Simply do:

简单地做:

df_conbid_N_1 = pd.read_csv("test-2019.csv",dtype=str, sep=';', encoding='utf-8')
df = df.fillna('')