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

巧了我就是萌 提交于 2021-02-07 09:27:00

问题


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?

Simplified Example:

Note: nan in col4 are not strings

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 "":

ID  col1   col2   col3   col4
1   Apple                nan
2          orange        nan
3                 banana nan

Any idea how to solve this problem?

Thanks,


回答1:


Use a list of strings to replace with blanks strings that won't affect actual nan's...

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



回答2:


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).

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




回答3:


You can compare each column to the numpy object dtype (which is basically text columns), and then only do replacements for those columns.

for col in df:
    if df[col] == np.dtype('O'):  # Object
        df.col1.replace(['None', 'NaN', np.nan], "", inplace=True)



回答4:


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)


来源:https://stackoverflow.com/questions/38922952/how-to-replace-all-the-nan-strings-with-empty-string-in-my-dataframe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!