0

I have a big data frame and I want to filter it according to the COUNTRY column. I need the data from European countries and I wrote :

country=['Austria', 'Belgium', 'Bulgaria', 'Croatia', 'Republic of Cyprus', 'Czech Republic', 
'Denmark', 'Estonia', 'Finland', 'France', 'Germany', 'Greece', 'Hungary', 'Ireland', 'Italy', 
'Latvia', 'Lithuania', 'Luxembourg', 'Malta', 'Netherlands', 'Poland', 'Portugal', 'Romania', 
'Slovakia', 'Slovenia', 'Spain' , 'Sweden']
filtered_df = Data[Data.COUNTRY.isin(country)]'

but it doesn't show the rows of these mentioned countries in data frame! any idea?

1 Answers1

2

I would use query, here's an example using the iris dataset.

iris = pd.read_csv('https://raw.githubusercontent.com/mwaskom/seaborn-data/master/iris.csv')
x1 = ['setosa', 'versicolor']
iris.query('species in @x1')

So for you it would be

Data.query('COUNTRY in @country')
Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
Quixotic22
  • 2,894
  • 1
  • 6
  • 14
  • `country=['Austria', 'Belgium', 'Bulgaria', 'Croatia', 'Republic of Cyprus', 'Czech Republic', 'Denmark', 'Estonia', 'Finland', 'France', 'Germany', 'Greece', 'Hungary', 'Ireland', 'Italy', 'Latvia', 'Lithuania', 'Luxembourg', 'Malta', 'Netherlands', 'Poland', 'Portugal', 'Romania', 'Slovakia', 'Slovenia', 'Spain' , 'Sweden'] Data.query('species in @country')` – Marzieh Karimi Nov 19 '21 at 14:48
  • but it does not work! – Marzieh Karimi Nov 19 '21 at 14:48
  • see edit, `species` was a column within `iris` – Quixotic22 Nov 19 '21 at 14:49