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

Drop records based on column sub-string

Goal: drop records from Dataframe, based on column 2, index 1, sub-string.

I have tried:

df = df[df[1] != '305-1']
df = df[df[1] != '305-2']

However, this is an absolute value, not a sub-string.

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

df:

    1   2   3
0   Emissions   305-1~GHG emissions in metric tons of CO2e~Gro...   Emissions for Gross direct (Scope 1) GHG emiss...
1   Emissions   305-3~GHG emissions in metric tons of CO2e~Bio...   Emissions for Biogenic CO2 emissions was 14681...
2   Emissions   305-2~Direct (Scope 1) GHG emissions by gas~CO2 Emissions for CO2 was 107973 tons in year 2014...
3   Emissions   305-2~Direct (Scope 1) GHG emissions by gas~N20 Emissions for N20 was 91661 tons in year 2014,...
4   Emissions   305-3~Direct (Scope 1) GHG emissions by gas~HFCs    Emissions for HFCs was 31744 tons in year 2014...

Desired output df:

1   Emissions   305-3~GHG emissions in metric tons of CO2e~Bio...   Emissions for Biogenic CO2 emissions was 14681...
4   Emissions   305-3~Direct (Scope 1) GHG emissions by gas~HFCs    Emissions for HFCs was 31744 tons in year 2014...

Please let me know if there is anything else I can add to post.

>Solution :

Use Series.str.contains with | for bitwise or with invert mask by ~:

df[~df[1].str.contains('305-1|305-2')]

Or with specidied values in []:

df[~df[1].str.contains('305-[12]')]
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