9951 explained code solutions for 126 technologies


python-pandasHow to join dataframes


import pandas as pd

df = pd.DataFrame({
  'Phone': ['ip5', 'ip6', 'ip8', 'sms', 'xi'],
  'Price': [204, 304, 404, 405, 305]
})

df_more = pd.DataFrame({
  'Phone': ['ip5', 'ip6', 'ip8', 'sms', 'xi'],
  'Color': ['red', 'black', 'red', 'red', 'black']
})

df = df.join(df_more.set_index('Phone'), on='Phone')ctrl + c
import pandas as pd

load Pandas module

df

first data frame

df_more

second data frame - the one to join to first

'Phone'

column name to join dataframes by


Usage example

import pandas as pd

df = pd.DataFrame({
  'Phone': ['ip5', 'ip6', 'ip8', 'sms', 'xi'],
  'Price': [204, 304, 404, 405, 305]
})

df_more = pd.DataFrame({
  'Phone': ['ip5', 'ip6', 'ip8', 'sms', 'xi'],
  'Color': ['red', 'black', 'red', 'red', 'black']
})

df = df.join(df_more.set_index('Phone'), on='Phone')

print(df)
output
  Phone  Price  Color
0   ip5    204    red
1   ip6    304  black
2   ip8    404    red
3   sms    405    red
4    xi    305  black