Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

python - Keeping NaN values and dropping nonmissing values

I have a DataFrame where I would like to keep the rows when a particular variable has a NaN value and drop the non-missing values.

Example:

ticker  opinion  x1       x2  
aapl    GC       100      70  
msft    NaN      50       40  
goog    GC       40       60  
wmt     GC       45       15  
abm     NaN      80       90  

In the above DataFrame, I would like to drop all observations where opinion is not missing (so, I would like to drop the rows where ticker is aapl, goog, and wmt).

Is there anything in pandas that is the opposite to .dropna()?

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

Use pandas.Series.isnull on the column to find the missing values and index with the result.

import pandas as pd

data = pd.DataFrame({'ticker': ['aapl', 'msft', 'goog'],
                     'opinion': ['GC', nan, 'GC'],
                     'x1': [100, 50, 40]})

data = data[data['opinion'].isnull()]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...