本文介绍了从 pandas 系列中删除NaN的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法从熊猫系列中删除NaN值?我有一个序列,其中可能有或可能没有一些NaN值,我想返回该序列的副本,其中删除了所有NaN.

Is there a way to remove a NaN values from a panda series? I have a series that may or may not have some NaN values in it, and I'd like to return a copy of the series with all the NaNs removed.

推荐答案

>>> s = pd.Series([1,2,3,4,np.NaN,5,np.NaN])
>>> s[~s.isnull()]
0    1
1    2
2    3
3    4
5    5

更新,甚至可以使用 pandas.Series.dropna() :

update or even better approach as @DSM suggested in comments, using pandas.Series.dropna():

>>> s.dropna()
0    1
1    2
2    3
3    4
5    5

这篇关于从 pandas 系列中删除NaN的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 11:07
查看更多