Python 使用seaborn打印时,如何处理缺少的值?

Python 使用seaborn打印时,如何处理缺少的值?,python,python-2.7,pandas,data-analysis,seaborn,Python,Python 2.7,Pandas,Data Analysis,Seaborn,我使用lambda以下函数将缺少的值替换为NaN: data=data.applymap(lambda x:np.nan如果是instance(x,basestring)和x.isspace()else x) ,其中data是我正在处理的数据帧 随后,我使用seaborn.distplot尝试绘制它的一个属性,如下所示: seaborn.distplot(data['alcconsumption'],hist=True,bins=100) plt.xlabel('AlcoholConsumpti

我使用lambda以下函数将缺少的值替换为NaN:

data=data.applymap(lambda x:np.nan如果是instance(x,basestring)和x.isspace()else x)

,其中data是我正在处理的数据帧

随后,我使用seaborn.distplot尝试绘制它的一个属性,如下所示:

seaborn.distplot(data['alcconsumption'],hist=True,bins=100)
plt.xlabel('AlcoholConsumption')
plt.ylabel('Frequency(normalized 0->1)')
它给了我以下错误:

AttributeError: max must be larger than min in range parameter.

在绘制数据之前,我肯定会处理缺少的值。ot是否不使用
dropna()
将完全取决于数据集的性质。
ALC消费
是数据帧的单个系列还是部分?在后一种情况下,使用
dropna()。缺少的值是少还是多?它们是分散在你的系列中,还是倾向于以小组的形式出现?是否有理由相信您的数据集中存在一种趋势

如果缺少的值很少且分散,则可以轻松地使用dropna()。在其他情况下,我会选择用以前观察到的值(1)填充缺失的值。或者甚至用插值(2)填充缺少的值。但是要小心!用填充或插值观测值替换大量数据可能会严重中断数据集,并导致非常错误的结论

下面是一些使用您的代码片段的示例

seaborn.distplot(data['alcconsumption'],hist=True,bins=100)
plt.xlabel('AlcoholConsumption')
plt.ylabel('Frequency(normalized 0->1)')
。。。在合成数据集上:

import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt

def sample(rows, names):
    ''' Function to create data sample with random returns

    Parameters
    ==========
    rows : number of rows in the dataframe
    names: list of names to represent assets

    Example
    =======

    >>> sample(rows = 2, names = ['A', 'B'])

                  A       B
    2017-01-01  0.0027  0.0075
    2017-01-02 -0.0050 -0.0024
    '''
    listVars= names
    rng = pd.date_range('1/1/2017', periods=rows, freq='D')
    df_temp = pd.DataFrame(np.random.randint(-100,100,size=(rows, len(listVars))), columns=listVars) 
    df_temp = df_temp.set_index(rng)


    return df_temp

df = sample(rows = 15, names = ['A', 'B'])
df['A'][8:12] = np.nan
df
输出:

            A   B
2017-01-01 -63.0  10
2017-01-02  49.0  79
2017-01-03 -55.0  59
2017-01-04  89.0  34
2017-01-05 -13.0 -80
2017-01-06  36.0  90
2017-01-07 -41.0  86
2017-01-08  10.0 -81
2017-01-09   NaN -61
2017-01-10   NaN -80
2017-01-11   NaN -39
2017-01-12   NaN  24
2017-01-13 -73.0 -25
2017-01-14 -40.0  86
2017-01-15  97.0  60
(1) 使用前向填充

ffill
将“向前填充值”,这意味着它将用上面行的值替换
nan

df = df['A'].fillna(axis=0, method='ffill')
sns.distplot(df, hist=True,bins=5)
plt.xlabel('AlcoholConsumption')
plt.ylabel('Frequency(normalized 0->1)')

(2) 使用插值

根据不同的方法插值。时间插值在每日和更高分辨率的数据上工作,以插值给定的间隔长度

df['A'] = df['A'].interpolate(method = 'time')
sns.distplot(df['A'], hist=True,bins=5)
plt.xlabel('AlcoholConsumption')
plt.ylabel('Frequency(normalized 0->1)')


如您所见,不同的方法呈现两种截然不同的结果。我希望这对你有用。如果没有,请告诉我,我会再次查看。

这是matplotlib/pylab直方图的已知问题

见例

在建议各种解决方法的情况下,有两种最受欢迎的方法(例如来自):

或者,指定箱子边缘(在这种情况下,无论如何都要使用
Anan
…):


可以使用以下行为使用seaborn的分布图选择非NaN值:

seaborn.distplot(data['alcconsumption'].notnull(),hist=True,bins=100)

为什么不在作图前删除它们?如何?我指的是哪个函数?<代码>数据[ alcCuff]?DROPNA()/Case> @ DATAVANCI,如果我的建议是有用的,你会考虑将它标记为接受的答案吗?[ DISPROCH ]和版本1.11一样,SeBrn说:“这个函数被弃用,将来的版本中将被删除。”
Amin=min(Anan)
Amax=max(Anan)
seaborn.distplot(A,hist=True,bins=np.linspace(Amin,Amax,nbins))
seaborn.distplot(data['alcconsumption'].notnull(),hist=True,bins=100)