9951 explained code solutions for 126 technologies


python-pandasHow to inner 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'],
  'Color': ['red', 'black']
})

df = df.join(df_more.set_index('Phone'), on='Phone', how='inner')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

how='inner'

final dataframe will keep only those rows present in both dataframes


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'],
  'Color': ['red', 'black']
})

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

print(df)
output
  Phone  Price  Color
0   ip5    204    red
1   ip6    304  black