how to collapse columns in pandas on null values?

后端 未结 6 1963
醉酒成梦
醉酒成梦 2021-01-14 02:18

Suppose I have the following dataframe:

pd.DataFrame({\'col1\':    [\"a\", \"a\", np.nan, np.nan, np.nan],
            \'override1\': [\"b\", np.nan, \"b\",          


        
6条回答
  •  时光说笑
    2021-01-14 02:44

    import pandas as pd
    import numpy as np
    df=pd.DataFrame({'col1':    ["a", "a", np.nan, np.nan, np.nan],
                'override1': ["b", np.nan, "b", np.nan, np.nan],
                'override2': ["c", np.nan, np.nan, "c", np.nan]})
    
    print(df)
    df=df['col1'].fillna('') + df['override1'].fillna('')+ df['override2'].fillna('')
    print(df)
    

提交回复
热议问题