忽略数据框中的 NaN [英] Ignoring NaN in a dataframe

查看:33
本文介绍了忽略数据框中的 NaN的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

推荐答案

尝试在调用 .unique() 之前立即调用 .dropna().一个工作示例:

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

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.])

这篇关于忽略数据框中的 NaN的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆