Skip to content Skip to sidebar Skip to footer

Updating A Dataframe Based On Another Dataframe

Given DataFrame df: Id Sex Group Time Time! 0 21 M 2 2.31 NaN 1 2 F 2 2.29 NaN and update: Id Sex Group Time 0 21 M 2 2.36 1 2 F

Solution 1:

I think I would do this with a merge, and then update the columns with a where. First remove the Time column from up:

In [11]: times = up.pop('Time')# up = the update DataFrame

In [12]: df1 = df.merge(up, how='outer')

In [13]: df1
Out[13]:
   Id Sex  Group  Time  Time!021   M      22.31NaN12F22.29NaN23F1NaNNaN

Update Time if it's not NaN and Time! if it's NaN:

In [14]: df1['Time!'] = df1['Time'].where(df1['Time'].isnull(), times)

In [15]: df1['Time'] = df1['Time'].where(df1['Time'].notnull(), times)

In [16]: df1
Out[16]:
   Id Sex  GroupTimeTime!021   M      22.312.3612   F      22.292.0923   F      11.79    NaN

Post a Comment for "Updating A Dataframe Based On Another Dataframe"