Home > Blockchain >  Combining pandas dataframes in this way
Combining pandas dataframes in this way

Time:10-25

I have a dataframe df1 :-

REGION DATE Count TIME PER ID
ABC 2021-03-22 2 44

I have another dataframe df2 :-

ID REGION DATE TIME
11 ABC 2021-03-22 198
75 ABC 2021-03-22 250

I want to achieve this :-

ID REGION DATE TIME TIME PER ID TOTAL TIME
11 ABC 2021-03-22 198 44 242
75 ABC 2021-03-22 250 44 294

Essentially I want to match the REGION and DATE and whatever value for TIME PER ID from df1 I want to populate that for those rows in df2 which matches the region and Date

CodePudding user response:

Merge both dataframes and then create the new column.

output_df = df2.merge(df1, on=['REGION', 'DATE'], how='left')
output_df.loc[:, 'TOTAL'] = output_df['Time']   output_df['TIME PER ID']
  • Related