Berkay
Berkay

Reputation: 345

Python/Pandas searching data in Dataframe

I want to explain my question with an example. I have a dataset which includes avocado average prices and many features about these prices(I guess avocado prices dataset is very popular, idk). And there is a feature called "region" that shows where avocadoes grew. I wrote this line of code to get to avocados feature which grews on "west". my data's name is data btw

west = data[data['region'] =='West'] 

And i thinked that what if i wanted to get the avocadoes which grew in 2016 and also grew on West. How can i get these data at the same time ?

Upvotes: 1

Views: 216

Answers (3)

Boskosnitch
Boskosnitch

Reputation: 774

west = data[(data['region']=='west')&(data['year']==2016)]

Upvotes: 0

v2sciences
v2sciences

Reputation: 56

I think the pandas DataFrame filter with boolean conditions can solve your question.

Suppose your column name for avocado growing year is grew_in. Then try this:

west_2016 = data[(data['region'] =='West') & (data['grew_in'] == 2016)] 

Upvotes: 2

Hrishikesh
Hrishikesh

Reputation: 1183

You can try query interfrace of pandas.

In particular, if your "grew in" data is present in year column, you could do something like,

data.query('region == "West" and year == 2016')

References:

Upvotes: 1

Related Questions