Skip to content
Advertisement

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 ?

Advertisement

Answer

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)] 
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement