简体   繁体   English

使用 pandas 从未命名或空白的列中删除行

[英]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.

相关问题 Python/Pandas - 删除第一行未命名:0,未命名:1,未命名:2,未命名:3,未命名:4,未命名:5,未命名:6,未命名:7 - Python/Pandas - Remove the first row with Unnamed: 0,Unnamed: 1,Unnamed: 2,Unnamed: 3,Unnamed: 4,Unnamed: 5,Unnamed: 6,Unnamed: 7 使用熊猫使用另一列中的值填充特定列中的空白行 - Fill Blank Row in a specific column with value from another column using pandas 使用熊猫根据列删除整个行 - Delete entire row based on column using pandas 从熊猫数据框中删除行和列 - Delete row and column from pandas dataframe 从json创建的Pandas数据框有未命名的列 - 由于未命名的列问题而无法插入MySQL - Pandas dataframe created from json has unnamed column - can't insert into MySQL due to unnamed column issue 为什么熊猫数据框将列名显示为'未命名:1',未命名:2',.......'未命名:n' - Why pandas dataframe displaying column names as 'unnamed: 1', unnamed: 2',.......'unnamed: n' 从 csv 文件 Pandas Python 中删除未命名的列 - Deleting an unnamed column from a csv file Pandas Python 使用Python从包含空白字段的文件中删除行 - Delete row from a file with blank fields using Python "重命名未命名的列熊猫数据框" - Rename unnamed column pandas dataframe Pandas:将一列与空白的行重叠 - Pandas: overlay a column into row with a blank one
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM