Python Forum
Filtering a data frame according to number of occurences of an observation
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Filtering a data frame according to number of occurences of an observation
#1
Hi,
I have a data frame that looks like
COL1
A
A
A
A
A
B
B
B
B
B
C
D
D
D
D
E
E

I 'd like to keep observations that appear more than 3 times only. So in that case, delete the rows that have the C and the Es in COL1.

I tried something like
df_test = df_have[df_have.groupby('COL1').count() >= 3]
but doesn't work.

Any idea ? Thank you by advance ! :)
Reply
#2
Is the list actually sorted as you have shown? It is much easier if it is.
What you do is count the number of elements, and when the element changes, store (or just print) the tuple <element, count>, but only if count > 3. Done.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Create X Number of Variables and Assign Data RockBlok 8 922 Nov-14-2023, 08:46 AM
Last Post: perfringo
  how do you style data frame that has empty rows. gsaray101 0 522 Sep-08-2023, 05:20 PM
Last Post: gsaray101
  googletrans library to translate text language for using data frame is not running gcozba2023 0 1,216 Mar-06-2023, 09:50 AM
Last Post: gcozba2023
  Load multiple Jason data in one Data Frame vijays3 6 1,535 Aug-12-2022, 05:17 PM
Last Post: vijays3
  conditionals based on data frame mbrown009 1 891 Aug-12-2022, 08:18 AM
Last Post: Larz60+
  Give visit number ( or counter) within the same time frame balthordu 1 934 Jun-27-2022, 09:51 AM
Last Post: Larz60+
  Merging two Data Frame on a special case piku9290dgp 0 1,080 Mar-02-2022, 10:43 AM
Last Post: piku9290dgp
  Save data frame to .csv df.to.csv() mcva 1 1,524 Feb-03-2022, 07:05 PM
Last Post: mcva
  Move a particular row in pandas data frame to last row klllmmm 0 3,738 Dec-27-2021, 09:11 AM
Last Post: klllmmm
  Iterating Through Data Frame Rows JoeDainton123 2 2,897 Aug-09-2021, 07:01 AM
Last Post: Pedroski55

Forum Jump:

User Panel Messages

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