Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
381 views
in Technique[技术] by (71.8m points)

python - Pandas drop rows where all but one column is NaN

I am reading data from a csv file into a pandas dataframe. Now some of the rows contain data in only one column (can be any column) and rest columns are NaN. The actual data may also contain some NaNs, therefore I want to drop all such rows where exactly one column contains data and rest are NaNs.

Therefore neither df = df.dropna(how='all') nor df = df.dropna(how='any') solves my issue.

Sample dataframe:

1     2    3    ... 19   20
text  NaN  NaN  ... NaN  NaN
NaN   abc  NaN  ... NaN  NaN
NaN   NaN  xyz  ... NaN  NaN
a     b    c    ... NaN    z
.
.

What I want become:

1     2    3    ... 19   20
a     b    c    ... NaN    z
.
.
question from:https://stackoverflow.com/questions/65919332/pandas-drop-rows-where-all-but-one-column-is-nan

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...