从熊猫系列中删除NaN


80

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

Answers:



4

少量使用 np.nan ! = np.nan

s[s==s]
Out[953]: 
0    1.0
1    2.0
2    3.0
3    4.0
5    5.0
dtype: float64

更多信息

np.nan == np.nan
Out[954]: False

1

如果您的熊猫系列具有NaN,并希望将其删除(不丢失索引):

serie = serie.dropna()

# create data for example
data = np.array(['g', 'e', 'e', 'k', 's']) 
ser = pd.Series(data)
ser.replace('e', np.NAN)
print(ser)

0      g
1    NaN
2    NaN
3      k
4      s
dtype: object

# the code
ser = ser.dropna()
print(ser)

0    g
3    k
4    s
dtype: object
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.