multiple conditioned slicing (pandas dataframe)

770 views Asked by At

I have a dataframe that has various columns and rows of data. I want to select all rows where the Year column = 2015 and Month column = 7.

The following works:

new_result.loc[new_result['Year'] == 2015,:].loc[new_result['Month'] == 7,:]

However, is there a more elegant way to express the same thing? i.e. Fewer text because I can see how total text can get out of control for a multiple conditioned query.

1

There are 1 answers

0
vk1011 On BEST ANSWER
new_result[(new_result['Year']==2015) & (new_result['Month']==7)]

or

new_result.query("Year==2015 and Month==7")