Python Forum
Filtering Data Frame, with another value
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Filtering Data Frame, with another value
#1
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.
Reply
#2
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)
Reply
#3
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
Reply
#4
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: []
Reply
#5
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)
Reply
#6
Hi,

Yes it's a bool. Active is a Column Name, it has 'True' and 'Flase' values.
Reply
#7
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)
NewBiee likes this post
Reply
#8
(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'
Reply
#9
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?
Reply
#10
(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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Grouping in pandas/multi-index data frame Aleqsie 3 698 Jan-06-2024, 03:55 PM
Last Post: deanhystad
  Data Sorting and filtering(From an Excel File) PY_ALM 0 1,057 Jan-09-2023, 08:14 PM
Last Post: PY_ALM
  Exporting data frame to excel dyerlee91 0 1,638 Oct-05-2021, 11:34 AM
Last Post: dyerlee91
  Pandas Data frame column condition check based on length of the value aditi06 1 2,707 Jul-28-2021, 11:08 AM
Last Post: jefsummers
  Adding a new column to a Panda Data Frame rsherry8 2 2,133 Jun-06-2021, 06:49 PM
Last Post: jefsummers
  grouped data frame glitter 0 1,606 Feb-02-2021, 11:22 AM
Last Post: glitter
  how to filter data frame dynamically with the columns psahay 0 2,414 Aug-24-2020, 01:10 PM
Last Post: psahay
  Dropping Rows From A Data Frame Based On A Variable JoeDainton123 1 2,238 Aug-03-2020, 02:05 AM
Last Post: scidam
  How to shift data frame rows of specified column Mekala 0 1,914 Jul-21-2020, 02:42 PM
Last Post: Mekala
  HELP- DATA FRAME INTO TIME SERIES- BASIC bntayfur 0 1,771 Jul-11-2020, 09:04 PM
Last Post: bntayfur

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020