[英]Delete row from a column that is unnamed or blank using pandas
I have a dataframe, df, where I would like to delete a row from a column that is unnamed or blank using pandas.我有一个数据框 df,我想在其中使用 Pandas 从未命名或空白的列中删除一行。 I would like to delete the row that contains 'id'
我想删除包含“id”的行
Data数据
a b c
date 21 22 23
id
aa 2 3 4
bb 1 2 3
cc 5 5 5
Desired想要的
a b c
date 21 22 23
aa 2 3 4
bb 1 2 3
cc 5 5 5
Doing正在做
df[df[""].str.contains("id")==False]
or或者
df.drop(1)
However, the action is not executed and I do not get the desired result.但是,该操作没有执行,我没有得到想要的结果。 I am actively researching this.
我正在积极研究这个。 Any suggestions are appreciated
任何建议表示赞赏
Use dropna
:使用
dropna
:
>>> df.dropna(how='all', axis=0)
a b c
date 21.0 22.0 23.0
aa 2.0 3.0 4.0
bb 1.0 2.0 3.0
cc 5.0 5.0 5.0
Update更新
If the first column is not an index but a real column with an empty name, maybe you should use this version:如果第一列不是索引而是一个空名称的真实列,也许你应该使用这个版本:
>>> df[df.loc[:, df.columns.str.len().astype(bool)].notna().any(axis=1)]
a b c
0 date 21.0 22.0 23.0
2 aa 2.0 3.0 4.0
3 bb 1.0 2.0 3.0
4 cc 5.0 5.0 5.0
Or more simple, if your unnamed column is the first:或者更简单,如果您的未命名列是第一个:
>>> df[df.iloc[:, 1:].notna().any(axis=1)]
a b c
0 date 21.0 22.0 23.0
2 aa 2.0 3.0 4.0
3 bb 1.0 2.0 3.0
4 cc 5.0 5.0 5.0
Regardless, you have a row with space.无论如何,您有一排空间。 Way out is to query df for such rows and filter them out.
出路是查询 df 以获取此类行并将其过滤掉。 Space, indicates a dtype
object
.空格,表示一个数据类型
object
。 I would我会
df.where((df!=' ')).dropna()
a b c
date 21.0 22.0 23.0
aa 2.0 3.0 4.0
bb 1.0 2.0 3.0
cc 5.0 5.0 5.0
运行此命令,它将删除包含任何空值的所有列
data = data.dropna()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.