Combine dataframes based on non-unique key

时间:2019-03-19 15:03:18

标签: python pandas

I have three dataframes that I want to combine based on a given column.

Here's the example:

I'd like to combine these dataframes, df1 and df2 into combined_df

df1 = pd.DataFrame({
    'key' : ['a','b','c'],
    'value1' : ['d','e','f'],
    'value2': ['5','6','7']})

df2 = pd.DataFrame({
    'key' : ['a','a','a','b','c','c'],
    'value3' : ['52','16','2','89','7','24'] })

combined_df = pd.DataFrame({
              'key' : ['a','a','a','b','c','c'],
              'value3' : ['52','16','2','89','7','24'],
              'value1' : ['d','d','d','e','f','f'],
              'value2' : ['5','5','5','6','7','7']})

I've been creating a dictionary for each column, and using df['key'].map(dictionary), but this approach is not scaling well.

0 个答案:

没有答案