Replace None with NaN in pandas dataframe

后端 未结 5 1255
抹茶落季
抹茶落季 2021-01-30 00:53

I have table x:

        website
0   http://www.google.com/
1   http://www.yahoo.com
2   None

I want to replace python None with pa

相关标签:
5条回答
  • 2021-01-30 01:06

    The following line replaces None with NaN:

    df['column'].replace('None', np.nan, inplace=True)
    
    0 讨论(0)
  • 2021-01-30 01:09

    You can use DataFrame.fillna or Series.fillna which will replace the Python object None, not the string 'None'.

    import pandas as pd
    import numpy as np
    

    For dataframe:

    df = df.fillna(value=np.nan)
    

    For column or series:

    df.mycol.fillna(value=np.nan, inplace=True)
    
    0 讨论(0)
  • 2021-01-30 01:09

    If you use df.replace([None], np.nan, inplace=True), this changed all datetime objects with missing data to object dtypes. So now you may have broken queries unless you change them back to datetime which can be taxing depending on the size of your data.

    If you want to use this method, you can first identify the object dtype fields in your df and then replace the None:

    obj_columns = list(df.select_dtypes(include=['object']).columns.values)
    df[obj_columns] = df[obj_columns].replace([None], np.nan)
    
    0 讨论(0)
  • 2021-01-30 01:12

    Here's another option:

    df.replace(to_replace=[None], value=np.nan, inplace=True)
    
    0 讨论(0)
  • 2021-01-30 01:25
    DataFrame['Col_name'].replace("None", np.nan, inplace=True)
    
    0 讨论(0)
提交回复
热议问题