Issue
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
Solution
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
Answered By - Corralien
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.