Select rows with columns ['A','B'] where rows with column 'C' contain NaN values in python (Pandas)
Now I want to select column 'A' and column 'B' of data frame where column 'C' contains NaN values.
If all columns or only one column needs to be selected then I can do below,
df['A'][df['C'].isnull()]
or
df[df['C'].isnull()]
but I am not getting how to select multiple columns.
您可以在第一种形式中放置多个列名。
df[['A','B']][df['C'].isnull()]
You can use loc
, and select a list of columns:
df.loc[df['C'].isnull(), ['A','B']]
For example
>>> df = pd.DataFrame({'A':[1,2,3,4], 'B':[5,6,7,8], 'C':[np.nan,1,np.nan,2]})
>>> df
A B C
0 1 5 NaN
1 2 6 1.0
2 3 7 NaN
3 4 8 2.0
>>> df.loc[df['C'].isnull(), ['A','B']]
A B
0 1 5
2 3 7
I like dropna
and drop
, since we will not have copy warning when we forget add the .copy()
sub=df.dropna(subset=['C']).drop('C',1)
sub
Out[26]:
A B
1 2 6
3 4 8
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.