0

I want to join these two data frames in this particular way.

df:

    name ... time
0   Tom  ...  13
1   Tom  ...  16
2   Ben  ...  10
3   Ben  ...  12

and df1:

   name ... time
0   Tom  ...  8
1   Ben  ...  6

to make...

    name ... time time1
0   Tom  ...  13    8
1   Tom  ...  16    8
2   Ben  ...  10    6
3   Ben  ...  12    6

Basically df1 contains the fastest times of Tom and Ben and df contains all other times. I want to merge the dataframes so the fastest time is always on the same row as the other times. Any help would be appreciated. Thanks in advance..

toothsie
  • 245
  • 3
  • 10

1 Answers1

2

You simply do that with merge and then rename the column names as desired.

>>> df = pd.merge(df1, df2, how='outer', on='name')
>>> df
  name  time_x  time_y
0  Tom      13       8
1  Tom      16       8
2  Ben      10       6
3  Ben      12       6

Change the column names:

>>> df.rename(columns={'time_x': 'time', 'time_y': 'time1'}, inplace=True)
>>> df
  name  time  time1
0  Tom    13      8
1  Tom    16      8
2  Ben    10      6
3  Ben    12      6
Karn Kumar
  • 8,518
  • 3
  • 27
  • 53