简体   繁体   English

从DataFrame提取熊猫系列的非空部分

[英]Extracting non-null portion of pandas series from DataFrame

Say I have this DataFrame: 说我有这个DataFrame:

    C1    C2    C3
0    1   4.4    99
1    2   4.5   200
2    3   NaN    65
3    4   3.2   140

I want to extract only the column (series) C2 and only the non-null elements of that series. 我只想提取C2列(系列),而只提取该系列的非null元素。

I can do that in two steps: 我可以分两个步骤进行操作:

d = df.loc[df['C2'].notnull()]
s=d['C2']

s will be s将是

0    4.4
1    4.5
3    3.2

Can I do this in one step? 我可以一步一步完成吗?

You are talking about dropna 您在谈论dropna

df.C2.dropna()
Out[486]: 
0    4.4
1    4.5
3    3.2
Name: C2, dtype: float64

Consider using query with .notna if you want to filter your dataframe: 如果要过滤数据.notna ,请考虑对.notna使用query

df.query('C2.notna()')

Output 输出量

   C1   C2   C3
0   1  4.4   99
1   2  4.5  200
3   4  3.2  140

您也可以将两个步骤组合为一个步骤并放下位置

df[df['C2'].notnull()]['C2']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM