0

I have 3 Data Frames

df1 = pd.DataFrame({'sev_group': {0: '1&2', 1: '3', 2: '4&5'},'Age': {0: '1.0', 1: '10.0', 2: '3.0'}})

df2 = pd.DataFrame({'sev_group': {0: '1&2', 1: '3', 2: '4&5'}, 'Age': {0: '0.0', 1: '24.0', 2: '5.0'}})

df3 = pd.DataFrame({'Count': {'1&2': '7', '3': '82', '4&5': '15'}, 'SLA': {'1&2': '1', '3': '3', '4&5': '5'}, 'Open': {'1&2': '0', '3': '8', '4&5': '5'}, 'Closed/Resolved': {'1&2': '7', '3': '74', '4&5': '10'}})

All the above column values are in dtype object.

df1

enter image description here

df2

enter image description here

df3

enter image description here

I need to add join Age column from df1 as df1Age and df2 as df2Age to df3.

Expected output

enter image description here

Sathish Kumar
  • 43
  • 1
  • 5

1 Answers1

1

Use the following code:

result = df3.join(df1.set_index('sev_group').Age.rename('df1Age'))\
    .join(df2.set_index('sev_group').Age.rename('df2ge'))

The result is:

    Count SLA Open Closed/Resolved df1Age df2ge
1&2     7   1    0               7    1.0   0.0
3      82   3    8              74   10.0  24.0
4&5    15   5    5              10    3.0   5.0

By the way: Your pictures for content of df1 and df2 are in wrong order, compared to your code sample to create these DataFrames.

Valdi_Bo
  • 24,530
  • 2
  • 17
  • 30