Python Forum

Full Version: Filtering Data Frame, with another value
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have a data frame with three columns (Active, UserName, Role):

Quote:Active UserName Role
FALSE 945480 Invoice Viewer
FALSE 945480 Maintain Invoice Streams
FALSE 945480 Maintain Payment Instruments
FALSE ClientAppAB Superuser
FALSE ClientAppBC Superuser
FALSE ClientAppPC Superuser
TRUE m002mb8 Maintain Invoice Streams
TRUE m002mb8 Maintain Payment Instruments
FALSE M003808 Maintain Invoice Streams
FALSE M003808 Maintain Payment Instruments
TRUE M003818 Maintain Invoice Streams
TRUE M003818 Maintain Payment Instruments
FALSE M003832 Maintain Invoice Streams
FALSE M003832 Maintain Payment Instruments
FALSE M003833 Maintain Invoice Streams
FALSE M003833 Maintain Payment Instruments

I'm trying to filter it with only Active values:

mask = df['Active'] == 'False'
df_new = pd.DataFrame(df[mask])
print(df_new)
But I receive empty DataFrame

Output:
Empty DataFrame Columns: [Active, UserName, Role] Index: []
Please help.
Active is 'TRUE' or 'FALSE', never 'False'. Your mask is all set to False. No rows meet the criteria.

When you correct the mask, this will work.
df_new = pd.DataFrame(df[mask])
But it makes an extra dataframe that you are just throwing away. This works without making an extra dataframe.
df_new = df[mask]
And this is the commonly used syntax for what you are doing.
df_new = df[df['Active] == 'FALSE']
print(df_new)
Are you looking for values with Active=TRUE, or Active =FALSE ? Just checking.

Also minor correction to last post:
df_new = df[df['Active'] == 'FALSE']
print(df_new)
There was a missing ' around Active
Hi,

Just used this:

df = pd.read_excel(r"Test_2.xlsx")

df_new = df[df['Active'] == 'TRUE']
print(df_new)
But I'm still getting the same results:

Output:
Empty DataFrame Columns: [Active, UserName, Role] Index: []
Try this:
df = pd.read_excel(r"Test_2.xlsx")
print(df.dtypes)
What does it say for "Active"? Is it bool or object?

If bool:
df = pd.read_excel(r"Test_2.xlsx")
 
df_new = df[df['Active']]
print(df_new)
Hi,

Yes it's a bool. Active is a Column Name, it has 'True' and 'Flase' values.
And if you wanted to selecte df['Active'] == False you could do this.
df = pd.read_excel(r"Test_2.xlsx")
  
df_new = df[~df['Active']]
print(df_new)
(Aug-18-2023, 01:03 PM)deanhystad Wrote: [ -> ]And if you wanted to selecte df['Active'] == False you could do this.
df = pd.read_excel(r"Test_2.xlsx")
  
df_new = df[~df['Active']]
print(df_new)

Hi,

I'm getting this error trying to run your code:

Output:
result = func(self.values, **kwargs) TypeError: bad operand type for unary ~: 'str'
The errror message says the dtype for the Active column is "object", not "bool", yet you said in an earlier post.
Output:
Yes it's a bool. Active is a Column Name, it has 'True' and 'Flase' values.
So what is it?
(Aug-21-2023, 09:14 AM)deanhystad Wrote: [ -> ]The errror message says the dtype for the Active column is "object", not "bool", yet you said in an earlier post.
Output:
Yes it's a bool. Active is a Column Name, it has 'True' and 'Flase' values.
So what is it?

Hi,

The code is working, I just ran the code, didn't realize I was running the wrong .py on my PyCharm.

Thanks