Pandas DataFrame - Replace NULL String with Blank and NULL Numeric with 0

前端 未结 2 2036

I am working on a large dataset with many columns of different types. There are a mix of numeric values and strings with some NULL values. I need to change the NULL Value to

2条回答
  •  梦毁少年i
    2021-01-14 02:06

    You could try this to substitute a different value for each different column (A to C are numeric, while D is a string):

    import pandas as pd
    import numpy as np
    
    df_pd = pd.DataFrame([[np.nan, 2, np.nan, '0'],
            [3, 4, np.nan, '1'],
            [np.nan, np.nan, np.nan, '5'],
            [np.nan, 3, np.nan, np.nan]],
            columns=list('ABCD'))
    
    df_pd.fillna(value={'A':0.0,'B':0.0,'C':0.0,'D':''})
    

提交回复
热议问题