Home > database >  Delete row from a column that is unnamed or blank using pandas
Delete row from a column that is unnamed or blank using pandas

Time:10-31

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

CodePudding user response:

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

CodePudding user response:

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

CodePudding user response:

Run this command, it will drop all column which contain any null values

data = data.dropna()
  • Related