重新采样 pandas 数据框并应用模式

oly*_*ska 2 python mode dataframe pandas

我想计算 pandas 数据框中每组重新采样的行的模式。我尝试这样:

import datetime
import pandas as pd
import numpy as np
from statistics import mode


date_times = pd.date_range(datetime.datetime(2012, 4, 5),
                           datetime.datetime(2013, 4, 5),
                           freq='D')
a = np.random.sample(date_times.size) * 10.0

frame = pd.DataFrame(data={'a': a},
                     index=date_times)

frame['b'] = np.random.randint(1, 3, frame.shape[0])
frame.resample("M").apply({'a':'sum', 'b':'mode'})
Run Code Online (Sandbox Code Playgroud)

但这不起作用。

我也尝试:

frame.resample("M").apply({'a':'sum', 'b':lambda x: mode(frame['b'])})
Run Code Online (Sandbox Code Playgroud)

但我得到了错误的结果。有任何想法吗?

谢谢。

unu*_*tbu 6

frame.resample("M").apply({'a':'sum', 'b':lambda x: mode(frame['b'])})lambda 函数中,每个重采样组都会调用一次。x分配给一个系列,其值来自 b重采样组的列。

lambda x: mode(frame['b'])忽略并简单地返回整个列的x众数。frame['b']

相反,你会想要类似的东西

frame.resample("M").apply({'a':'sum', 'b':lambda x: mode(x)})
Run Code Online (Sandbox Code Playgroud)

然而,这会导致StatisticsError

StatisticsError: no unique mode; found 2 equally common values
Run Code Online (Sandbox Code Playgroud)

因为存在一个具有多个最常见值的重采样组。

如果您scipy.stats.mode改为使用,则返回最小的最常见值:

import datetime
import pandas as pd
import numpy as np
import scipy.stats as stats

date_times = pd.date_range(datetime.datetime(2012, 4, 5),
                           datetime.datetime(2013, 4, 5),
                           freq='D')
a = np.random.sample(date_times.size) * 10.0
frame = pd.DataFrame(data={'a': a}, index=date_times)
frame['b'] = np.random.randint(1, 3, frame.shape[0])

result = frame.resample("M").apply({'a':'sum', 'b':lambda x: stats.mode(x)[0]})
print(result)
Run Code Online (Sandbox Code Playgroud)

产量

            b           a
2012-04-30  2  132.708704
2012-05-31  2  149.103439
2012-06-30  2  128.492203
2012-07-31  2  142.167672
2012-08-31  2  126.516689
2012-09-30  1  133.209314
2012-10-31  2  136.684212
2012-11-30  2  165.075150
2012-12-31  2  167.064212
2013-01-31  1  150.293293
2013-02-28  1  125.533830
2013-03-31  2  174.236113
2013-04-30  2   11.254136
Run Code Online (Sandbox Code Playgroud)

如果您想要最大的最常见值,那么不幸的是,我不知道有任何内置函数可以为您执行此操作。在这种情况下,您可能需要计算一个value_counts表:

In [89]: counts
Out[89]: 
            b  counts
2012-04-30  3      11
2012-04-30  2      10
2012-04-30  1       5
2012-05-31  2      14
2012-05-31  1       9
2012-05-31  3       8
Run Code Online (Sandbox Code Playgroud)

然后按和值降序排序,按日期分组并取每组中的第一个值:countsb

import datetime as DT
import numpy as np
import scipy.stats as stats
import pandas as pd
np.random.seed(2018)

date_times = pd.date_range(DT.datetime(2012, 4, 5), DT.datetime(2013, 4, 5), freq='D')
N = date_times.size
a = np.random.sample(N) * 10.0
frame = pd.DataFrame(data={'a': a, 'b': np.random.randint(1, 4, N)}, index=date_times)

resampled = frame.resample("M")
sums = resampled['a'].sum()
counts = resampled['b'].value_counts()
counts.name = 'counts'
counts = counts.reset_index(level=1)
counts = counts.sort_values(by=['counts','b'], 
                             ascending=[False,False])
result = counts.groupby(level=0).first()
Run Code Online (Sandbox Code Playgroud)

产量

            b  counts
2012-04-30  3      11
2012-05-31  2      14
2012-06-30  3      12
2012-07-31  2      12
2012-08-31  2      11
2012-09-30  3      12
2012-10-31  2      13
2012-11-30  3      13
2012-12-31  2      14
2013-01-31  3      14
2013-02-28  1      10
2013-03-31  3      13
2013-04-30  3       2
Run Code Online (Sandbox Code Playgroud)