Home > OS >  Removing rows in DF based on counting filtered data pandas
Removing rows in DF based on counting filtered data pandas

Time:12-21

I have a dataframe that contains ID & month of transaction.

I want to keep only the stores that have transactions in 12 months.

I tried first to filter by unique as follows:

df.groupby('STORE_NBR')['MONTH'].nunique()

I got from the code the store ID and the number of months. The problem is not all stores IDs appeared so I couldn't get them to drop.

sample of data : enter image description here

CodePudding user response:

Try this:

df.groupby('STORE_NBR').filter(lambda group: group['MONTH'].nunique() >= 12)
  • Related