0

I have 2 csv file. They have one common column which is ID. What I want to do is I want to extract the common rows and built another dataframe. Firstly, I want to select job, and after that, as I said they have one common column, I want to find the rows whose IDs are the same. Visually, the dataframe should be seen like this:

Let first DataFrame is:

#ID #Gender #Job #Shift #Wage
1 Male Engineer Night 8000
2 Male Engineer Night 7865
3 Female Worker Day 5870
4 Male Accountant Day 5870
5 Female Architecture Day 4900

Let second one is:

#ID #Department
1 IT
2 Quality Control
5 Construction
7 Construction
8 Human Resources

And the new DataFrame should be like:

#ID #Department #Job #Wage
1 IT Engineer 8000
2 Quality Control Engineer 7865
5 Construction Architecture 4900
akoluacik
  • 33
  • 5

2 Answers2

2

You can use:

df_result = df1.merge(df2, on = 'ID', how = 'inner')

If you want to select only certain columns from a certain df use:

df_result = df1[['ID','Job', 'Wage']].merge(df2[['ID', 'Department']], on = `ID`, how = 'inner')
Jonas Palačionis
  • 4,591
  • 4
  • 22
  • 55
1

Use:

df = df2.merge(df1[['ID','Job', 'Wage']], on='ID')
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252