2

I am trying to find median revenue in 2013 for US, France and Spain. My pandas dataframe looks like enter image description here

I am using the following code

 df[(df.year == 2013) & (df.country == ['US', 'FR', 'ES'])]

and getting this error - ValueError: Lengths must match to compare

azro
  • 53,056
  • 7
  • 34
  • 70
Yash
  • 319
  • 1
  • 4
  • 6

2 Answers2

3

To filter a value between different possibilities, use Series.isin

df[(df.year == 2013) & (df.country.isin(['US', 'FR', 'ES']))]
azro
  • 53,056
  • 7
  • 34
  • 70
1

you are comparing a pandas series to a list, what pandas understands is that you want to get a mask of values equality item by item with the list, so it requires the list to be the same length as the pandas series object, to find out if the string is one of the following (or has substring of them), try this instead:

df[(df.year == 2013) & (df.country.str.conatins('|'.join(['US', 'FR', 'ES']))]

UPDATE

the other answer by @azro is more relevant because it checks equality instead of contain, so... at least I have tried :)

adir abargil
  • 5,495
  • 3
  • 19
  • 29