KansaiRobot
KansaiRobot

Reputation: 9912

Filtering a dataframe with the values of another dataframe

I have a bit of a complicated selecting in two dataframes.

Say I have a dataframe like this

       country         pop continent  lifeExp    gdpPercap
0  Afghanistan  31889923.0      Asia   43.828   974.580338
1      Albania   3600523.0    Europe   76.423  5937.029526
2      Algeria  33333216.0    Africa   72.301  6223.367465

(Actually this dataframe is product of a previous filtering I did)

And I have another dataframe like this

country         year         Production
Afghanistan    1980          20
Afghanistan    1981          10
Afghanistan    1983          5
Albania        1970         30
Albania        1980         40
Abisinia       1990          3
Algeria        1990         30
Angola          1980         2
Angola         1982         30

So what I want to get is the second dataframe but filtered with only the countries present in the first dataframe..(Afghanistan, Albania and Algeria)

What would be the best pythonic way to get this (hopefully in a fast operation)?

Upvotes: 0

Views: 1255

Answers (1)

Timeless
Timeless

Reputation: 37787

You can use boolean indexing with pandas.DataFrame.loc and pandas.Series.isin :

out= df2.loc[df2["country"].isin(df1["country"])]

# Output :

print(out)

       country  year  Production
0  Afghanistan  1980          20
1  Afghanistan  1981          10
2  Afghanistan  1983           5
3      Albania  1970          30
4      Albania  1980          40
6      Algeria  1990          30

Upvotes: 4

Related Questions