-2

I want to create a pandas dataframe df1 with specific column name from a column col of another dataframe df and do a merge with another dataframe df2.

df
    Name   House
0   John   London
1   John   London
2   John   London
3   Tom    New York
4   Tom    New York

df2
     Col  Val
0    Tom    3
1    John   2
2    Alex   5
3    Sarah  2

This what I am doing

import pandas as pd
x = pd.unique(df['Name'])
x = pd.DataFrame(x)
x.columns = ['col']
df1 = pd.merge(x, df2, on = 'Col')

df1
    Col  Val 
0   Tom    3
1   John   2
petezurich
  • 9,280
  • 9
  • 43
  • 57
emax
  • 6,965
  • 19
  • 74
  • 141

1 Answers1

-1

Are you just looking for a better way to do what you're doing? This is what I generally do when I need to filter a dataframe.

import pandas as pd
names = set(df['Name'].values)
smaller_df = df2[df2['Col'].isin(names)]

Edited because I didn't understand OP's question.

killian95
  • 803
  • 6
  • 11