pandas 忽略数据框中的 NaN

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44681776/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 03:51:04  来源:igfitidea点击:

Ignoring NaN in a dataframe

pythonpandasnumpydataframe

提问by mihir shanvir

I want to find the unique elements in a column of a dataframe which have missing values. i tried this: df[Column_name].unique()but it returns nan as one of the elements. what can i do to just ignore the missing values. dataframe look like this.click here

我想在数据框的一列中找到具有缺失值的唯一元素。我试过这个:df[Column_name].unique()但它返回 nan 作为元素之一。我能做些什么来忽略缺失的值。数据框看起来像这样。点击这里

回答by Peter Leimbigler

Try calling .dropna()right before your call to .unique(). A working example:

尝试.dropna()在您致电 之前立即致电.unique()。一个工作示例:

import pandas as pd
import numpy as np
df = pd.DataFrame({'col1': np.random.randint(0, 10, 12)})
df.loc[2] = np.nan
df.loc[5] = np.nan
df['col1'].unique()
### output: array([  4.,   0.,  nan,   8.,   1.,   3.,   2.,   6.])
df['col1'].dropna().unique()
### output: array([ 4.,  0.,  8.,  1.,  3.,  2.,  6.])