Overwrite columns in DataFrames of different sizes pandas

前端 未结 3 955
梦如初夏
梦如初夏 2021-01-13 13:20

I have following two Data Frames:

df1 = pd.DataFrame({\'ids\':[1,2,3,4,5],\'cost\':[0,0,1,1,0]})
df2 = pd.DataFrame({\'ids\':[1,5],\'cost\':[1,4]})
         


        
3条回答
  •  说谎
    说谎 (楼主)
    2021-01-13 14:09

    Another way to do it, using a temporary merged dataframe which you can discard after use.

    import pandas as pd
    
    df1 = pd.DataFrame({'ids':[1,2,3,4,5],'cost':[0,0,1,1,0]})
    df2 = pd.DataFrame({'ids':[1,5],'cost':[1,4]})
    
    dftemp = df1.merge(df2,on='ids',how='left', suffixes=('','_r'))
    print(dftemp)
    
    df1.loc[~pd.isnull(dftemp.cost_r), 'cost'] = dftemp.loc[~pd.isnull(dftemp.cost_r), 'cost_r']
    del dftemp 
    
    df1 = df1[['ids','cost']]
    print(df1)
    
    
    OUTPUT-----:
    dftemp:
       cost  ids  cost_r
    0     0    1     1.0
    1     0    2     NaN
    2     1    3     NaN
    3     1    4     NaN
    4     0    5     4.0
    
    df1:
       ids  cost
    0    1   1.0
    1    2   0.0
    2    3   1.0
    3    4   1.0
    4    5   4.0
    

提交回复
热议问题