[英]Dropping rows based on index and condition
I want to drop certain rows based off of range of rows: I feel like it would look something like the below我想根据行范围删除某些行:我觉得它看起来像下面这样
df.drop(df.index[[0,1,2,3]])
so that will just drop my first two rows but I would like to drop those rows if and only if that range of rows is blank.这样只会删除我的前两行,但当且仅当该行范围为空白时,我才想删除这些行。 so
所以
Name
Macy
June
Jackson
So I would only want to go through the dataframe and drop rows in that range if and only if their is no data so my new dataframe would look like this:因此,当且仅当它们没有数据时,我只想遍历数据框并删除该范围内的行,因此我的新数据框将如下所示:
Name
Macy
June
Jackson
You don't need the df.index
in the drop statement.您不需要 drop 语句中的
df.index
。 However, if you only want to keep the rows where the Name
column is filled, you just can use:但是,如果您只想保留填充
Name
列的行,则可以使用:
> df_filter = df [df.Name != '']
If you really want to locate the rows based on index, you can use如果你真的想根据索引定位行,你可以使用
> df_filter = df.drop([0,2])
You can use df.ix
to get the excepted items, then drop the None
s using dropna()
method:您可以使用
df.ix
获取例外项目,然后使用dropna()
方法删除None
:
df.ix[indices].dropna()
Demo:演示:
In [48]: df
Out[48]:
name
0 None
1 Macy
2 June
3 None
4 Jackson
In [49]: df.ix[[0,1,2,3]].dropna()
Out[49]:
name
1 Macy
2 June
Note: As mentioned in comment, since the ix
method is deprecated you can use df.loc()
instead.注意:如评论中所述,由于不推荐使用
ix
方法,因此您可以使用df.loc()
代替。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.