Fillna in multiple columns in place in Python Pandas

后端 未结 6 1960
北海茫月
北海茫月 2020-12-23 11:33

I have a pandas dataFrame of mixed types, some are strings and some are numbers. I would like to replace the NAN values in string columns by \'.\', and the NAN values in flo

6条回答
  •  清酒与你
    2020-12-23 12:19

    You can either list the string columns by hand or glean them from df.dtypes. Once you have the list of string/object columns, you can call fillna on all those columns at once.

    # str_cols = ['Name','City']
    str_cols = df.columns[df.dtypes==object]
    df[str_cols] = df[str_cols].fillna('.')
    df = df.fillna(0)
    

提交回复
热议问题