Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Filtered Row Count
#1
I am trying to get a row count for a filtered data frame.

filt = (df['Type of breach']!='HACK') & (df['Description of incident'].str.contains('bank account', na=False) | df['Description of incident'].str.contains('social security number', na=False))

I have tried:

df.count()
len (filt)
df.shape

All of these return the count of rows for the entire data frame, not the count for the filtered results. How do I display the count for the remaining rows after filtration?
Reply
#2
I believe that I have been making progress:

seriesObj = df.apply(lambda x: True if x['Type of breach'] != 'HACK' else False , axis=1)

numOfRows = len(seriesObj[seriesObj == True].index)

print('Number of Rows in dataframe in which Type of breach != HACK : ', numOfRows)

These lines of code will return a count for only a partial filter (1 out of 2). How do I write a code to also include the other filter as well (to include the keywords "bank accounts" or "social security number" in the other column of my dataset)?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Row Count and coloumn count Yegor123 4 1,321 Oct-18-2022, 03:52 AM
Last Post: Yegor123

Forum Jump:

User Panel Messages

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