Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to delete rows per category in pandas based on a specific range? and the range is a string

I have a dataframe like this,

Date           Info 
2022-01-01     egg price
2022-01-01     Central Java
2022-01-01     East Java
2022-01-01     chicken price
2022-01-01     Central Java
2022-01-01     East Java
2022-01-02     egg price
2022-01-02     Central Java
2022-01-02     East Java
2022-01-02     chicken price
2022-01-02     Central Java
2022-01-02     East Java

how to delete rows starting from egg price to row before chicken price, and this is per date category.
I want to be like this:

Date           Info
2022-01-01     chicken price
2022-01-01     Central Java
2022-01-01     East Java
2022-01-02     chicken price
2022-01-02     Central Java
2022-01-02     East Java

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

If per Date are only one values egg and chicken price, egg is before chicken is possible create mask and get values between with GroupBy.cummax:

m1 = df['Info'].eq('egg price')
m2 = df['Info'].eq('chicken price')
mask = m1.groupby(df['Date']).cummax() & m2.iloc[::-1].groupby(df['Date']).cummax() & ~m2
df = df[~mask]

print (df)
          Date           Info
3   2022-01-01  chicken price
4   2022-01-01   Central Java
5   2022-01-01      East Java
9   2022-01-02  chicken price
10  2022-01-02   Central Java
11  2022-01-02      East Java
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading