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!
0  21   M      2  2.31    NaN
1   2   F      2  2.29    NaN
2   3   F      1   NaN    NaN

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  Group  Time  Time!
0  21   M      2  2.31   2.36
1   2   F      2  2.29   2.09
2   3   F      1  1.79    NaN

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