[英]How to drop row in pandas dataframe according to a condition on the index of the row
I have a dataframe called Prod with this shape:我有一个名为 Prod 的 dataframe,形状如下:
carriers electricity
techs
ETH_imp 0.000000e+00
T:Arusha 1.786273e+06
T:Dar_es_Salaam 0.000000e+00
T:Dodoma 3.348339e+08
T:Geita 0.000000e+00
ccgt 3.412390e+08
So my Prod.index
results in:所以我的Prod.index
结果:
Index(['ETH_imp', 'T:Arusha', 'T:Dar_es_Salaam', 'T:Dodoma', 'T:Geita', 'ccgt'],
dtype='object', name='techs')
I need to drop the rows, whose indexes contain the string 'T:'
我需要删除索引包含字符串'T:'
行
I tried Prod=Prod.drop(Prod['T:' in Prod.index].index)
, but apparently the boolean inside square parenthesis, 'T:' in Prod.index
only returns one false
我试过Prod=Prod.drop(Prod['T:' in Prod.index].index)
,但显然方括号内的 boolean , Prod.index 中'T:' in Prod.index
只返回一个false
You can change logic - get all rows if not index values contains or starting by T:
您可以更改逻辑 - 如果索引值不包含或以T:
So filter by boolean indexing
with ~
for invert mask with str.contains
:因此,通过boolean indexing
过滤~
用于带有str.contains
的反转掩码:
df1 = Prod[~Prod.index.str.contains('T:')]
Or str.startswith
:或str.startswith
:
df1 = Prod[~Prod.index.str.startswith('T:')]
print (df1)
electricity
carriers
ETH_imp 0.0
ccgt 341239000.0
You can also use Series.str.match
您也可以使用Series.str.match
new_df=Prod[~Prod.index.str.match('T:')]
print(new_df)
electricity
carriers
ETH_imp 0.0
ccgt 341239000.0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.