Filtering Pandas Dataframe using OR statement

PythonPandasFilterDataframe

Python Problem Overview


I have a pandas dataframe and I want to filter the whole df based on the value of two columns in the data frame. I want to get back all rows and columns where IBRD or IMF != 0.

alldata_balance = alldata[(alldata[IBRD] !=0) or (alldata[IMF] !=0)]

but this gives me a ValueError

> ValueError: The truth value of a Series is ambiguous. Use a.empty, > a.bool(), a.item(), a.any() or a.all().

So I know I am not using the or statement correctly, is there a way to do this?

Python Solutions


Solution 1 - Python

From the docs:

> Another common operation is the use of boolean vectors to filter the > data. The operators are: | for or, & for and, and ~ for not. These > must be grouped by using parentheses.

http://pandas.pydata.org/pandas-docs/version/0.15.2/indexing.html#boolean-indexing

Try:

alldata_balance = alldata[(alldata[IBRD] !=0) | (alldata[IMF] !=0)]

Solution 2 - Python

You can do like below to achieve your result:

import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
....
....
#use filter with plot
#or
fg=sns.factorplot('Retailer country', data=df1[(df1['Retailer country']=='United States') | (df1['Retailer country']=='France')], kind='count')

fg.set_xlabels('Retailer country')
plt.show()


#also
#and
fg=sns.factorplot('Retailer country', data=df1[(df1['Retailer country']=='United States') & (df1['Year']=='2013')], kind='count')

fg.set_xlabels('Retailer country')
plt.show()

Solution 3 - Python

Just wanted to note that you can use both of or and | inside the query method:

alldata.query('IBRD!=0 or IMF!=0')

and

alldata.query('IBRD!=0 | IMF!=0')

both produce the same outcome.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJoshView Question on Stackoverflow
Solution 1 - PythonLiam FoleyView Answer on Stackoverflow
Solution 2 - PythonMajedView Answer on Stackoverflow
Solution 3 - Pythonuser7864386View Answer on Stackoverflow