[英]Drop records based on column sub-string
Goal: drop records from Dataframe, based on column 2, index 1
, sub-string.目标:从 Dataframe 中删除记录,基于第 2 列, index 1
,子字符串。
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
: 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
:所需的 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.如果还有什么我可以添加到帖子中,请告诉我。
Use Series.str.contains
with |
使用Series.str.contains
和|
for bitwise or
with invert mask by ~
:对于按位or
通过~
反转掩码:
df[~df[1].str.contains('305-1|305-2')]
Or with specidied values in []
:或使用[]
中的指定值:
df[~df[1].str.contains('305-[12]')]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.