OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Compare two columns in different dataframes and filter each for common values with pandas

  • Thread starter Thread starter Yusuf Oddy
  • Start date Start date
Y

Yusuf Oddy

Guest
I have two dataframes, both with a country column. There are some countries that are not common to both datasets. I want to filter out these unique countries so that each dataframe now only contains countries that also exist in the other.

For instance, if Algeria is in dataframe A but not in dataframe B, I want the Algeria row removed from dataframe A. (And likewise for countries in B but not A)

I tried using isin, I need to remove the False rows - am I on the right track?:

Code:
x.Country.isin(y.Country)

83      True
111     True
0       True
4       True
67      True
       ...  
175    False
174    False
130    False
49      True
50      True
Name: Country, Length: 176, dtype: bool
<p>I have two dataframes, both with a country column. There are some countries that are not common to both datasets. I want to filter out these unique countries so that each dataframe now only contains countries that also exist in the other.</p>
<p>For instance, if Algeria is in dataframe A but not in dataframe B, I want the Algeria row removed from dataframe A. (And likewise for countries in B but not A)</p>
<p>I tried using isin, I need to remove the False rows - am I on the right track?:</p>
<pre><code>x.Country.isin(y.Country)

83 True
111 True
0 True
4 True
67 True
...
175 False
174 False
130 False
49 True
50 True
Name: Country, Length: 176, dtype: bool
</code></pre>
 
Top