简体   繁体   中英

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. I would like to delete the row that contains '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 :

>>> 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. Space, indicates a dtype 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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM