pandas: combine two columns in a DataFrame

后端 未结 5 505
感情败类
感情败类 2020-11-30 07:42

I have a pandas DataFrame that has multiple columns in it:

Index: 239897 entries, 2012-05-11 15:20:00 to 2012-06-02 23:44:51
Data columns:
foo           


        
5条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-30 08:08

    you can use directly fillna and assigning the result to the column 'bar'

    df['bar'].fillna(df['foo'], inplace=True)
    del df['foo']
    

    general example:

    import pandas as pd
    #creating the table with two missing values
    df1 = pd.DataFrame({'a':[1,2],'b':[3,4]}, index = [1,2])
    df2 = pd.DataFrame({'b':[5,6]}, index = [3,4])
    dftot = pd.concat((df1, df2))
    print dftot
    #creating the dataframe to fill the missing values
    filldf = pd.DataFrame({'a':[7,7,7,7]})
    
    #filling 
    print dftot.fillna(filldf)
    

提交回复
热议问题