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

1
  • Try: df[(df.year == 2013) & (df.country.isin(['US', 'FR', 'ES']))]
    – moo
    Commented Dec 28, 2020 at 18:48

2 Answers 2

4

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

df[(df.year == 2013) & (df.country.isin(['US', 'FR', 'ES']))]
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 :)

Not the answer you're looking for? Browse other questions tagged or ask your own question.