Home > Software design >  Drop records based on column sub-string
Drop records based on column sub-string

Time:11-23

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.

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.

CodePudding user response:

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]')]
  • Related