繁体   English   中英

最常用的值是使用pandas.DataFrame.resample

[英]Most frequent value using pandas.DataFrame.resample

我正在使用pandas.DataFrame.resample来重新采样带有时间戳索引的分组Pandas dataframe

在其中一列中,我想重新采样,以便选择最常用的值。 目前,我只是成功使用np.maxnp.sum等NumPy函数。

#generate test dataframe
data = np.random.randint(0,10,(366,2))
index = pd.date_range(start=pd.Timestamp('1-Dec-2012'), periods=366, unit='D')
test = pd.DataFrame(data, index=index)

#generate group array
group =  np.random.randint(0,2,(366,))

#define how dictionary for resample
how_dict = {0: np.max, 1: np.min}

#perform grouping and resample
test.groupby(group).resample('48 h',how=how_dict)

之前的代码有效,因为我使用了NumPy函数。 但是,如果我想以最常见的价值使用重新采样,我不确定。 我尝试定义一个自定义函数

def frequent(x):
    (value, counts) = np.unique(x, return_counts=True)
    return value[counts.argmax()]

但是,如果我现在这样做:

how_dict = {0: np.max, 1: frequent}

我得到一个空的数据帧......

df = test.groupby(group).resample('48 h',how=how_dict)
df.shape

您的重新采样周期太短,因此当一个组在一段时间内为空时,您的用户函数会引发一个不会被pandas捕获的ValueError。

但它没有空组,例如使用常规组:

In [8]: test.groupby(arange(366)%2).resample('48h',how=how_dict).head()
Out[8]: 
              0  1
0 2012-12-01  4  8
  2012-12-03  0  3
  2012-12-05  9  5
  2012-12-07  3  4
  2012-12-09  7  3

或者更长的时期:

In [9]: test.groupby(group).resample('122D',how=how_dict)
Out[9]: 
              0  1
0 2012-12-02  9  0
  2013-04-03  9  0
  2013-08-03  9  6
1 2012-12-01  9  3
  2013-04-02  9  7
  2013-08-02  9  1

编辑

解决方法可以是管理空案例:

def frequent(x):
    if len(x)==0 : return -1
    (value, counts) = np.unique(x, return_counts=True)
    return value[counts.argmax()]

对于

In [11]: test.groupby(group).resample('48h',how=how_dict).head()
Out[11]: 
               0  1
0 2012-12-01   5  3
  2012-12-03   3  4
  2012-12-05 NaN -1
  2012-12-07   5  0
  2012-12-09   1  4

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM