pandas DataFrame concat / update (“upsert”)?

后端 未结 2 1636
天涯浪人
天涯浪人 2020-12-13 15:02

I am looking for an elegant way to append all the rows from one DataFrame to another DataFrame (both DataFrames having the same index and column structure), but in cases whe

2条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-13 15:07

    In addition to the correct answer, watch out for columns that do not exist in both dataframes:

        df1 = pd.DataFrame([['test',1, True], ['test2',2, True]]).set_index(0)
        df2 = pd.DataFrame([['test2',4], ['test3',3]]).set_index(0)
    

    If you just use the aforementioned solution as-is, you get:

        >>>     1   2
        0       
        test    1   True
        test2   4   NaN
        test3   3   NaN
    

    But if you are expecting the following output:

        >>>     1   2
        0       
        test    1   True
        test2   4   True
        test3   3   NaN
    

    Just change the statement to:

        df1 = pd.concat([df1, df2[~df2.index.isin(df1.index)]])
        df1.update(df2)
    

提交回复
热议问题