【问题标题】:Pandas TimeGrouper and Pivot?Pandas TimeGrouper 和 Pivot?
【发布时间】:2017-06-28 03:30:36
【问题描述】:

这是我的数据框的样子:

  Timestamp               CAT
0 2016-12-02 23:35:28     200
1 2016-12-02 23:37:43     200
2 2016-12-02 23:40:49     300
3 2016-12-02 23:58:53     400
4 2016-12-02 23:59:02     300
...

这就是我在 Pandas 中尝试做的事情(注意时间戳是分组的):

Timestamp BINS         200   300   400   500
2016-12-02 23:30         2     0     0     0
2016-12-02 23:40         0     1     0     0
2016-12-02 23:50         0     1     1     0
...

我正在尝试创建 10 分钟时间间隔的 bin,以便制作条形图。并将列作为 CAT 值,因此我可以计算每个 CAT 在该时间段内出现的次数。

我目前所拥有的可以创建时间箱:

def create_hist(df, timestamp, freq, fontsize, outfile):
    """ Create a histogram of the number of CATs per time period."""

    df.set_index(timestamp,drop=False,inplace=True)
    to_plot = df[timestamp].groupby(pandas.TimeGrouper(freq=freq)).count()
    ...

但我的问题是我终其一生都无法弄清楚如何按 CAT 和按时间箱进行分组。我最近的尝试是在进行 groupby 之前使用df.pivot(columns="CAT"),但这只会给我错误:

def create_hist(df, timestamp, freq, fontsize, outfile):
    """ Create a histogram of the number of CATs per time period."""

    df.pivot(columns="CAT")
    df.set_index(timestamp,drop=False,inplace=True)
    to_plot = df[timestamp].groupby(pandas.TimeGrouper(freq=freq)).count()
    ...

这给了我:ValueError: Buffer has wrong number of dimensions (expected 1, got 2)

【问题讨论】:

    标签: python pandas pivot histogram pandas-groupby


    【解决方案1】:

    你也可以使用get_dummiesresample

    In [11]: df1 = df.set_index("Timestamp")
    
    In [12]: pd.get_dummies(df1["CAT"])
    Out[12]:
                         200  300  400
    Timestamp
    2016-12-02 23:35:28    1    0    0
    2016-12-02 23:37:43    1    0    0
    2016-12-02 23:40:49    0    1    0
    2016-12-02 23:58:53    0    0    1
    2016-12-02 23:59:02    0    1    0
    
    In [13]: pd.get_dummies(df1["CAT"]).resample("10min").sum()
    Out[13]:
                         200  300  400
    Timestamp
    2016-12-02 23:30:00    2    0    0
    2016-12-02 23:40:00    0    1    0
    2016-12-02 23:50:00    0    1    1
    

    【讨论】:

    • 这比我的干净多了。谢谢!
    【解决方案2】:

    使用pd.TimeGrouper

    df.set_index('Timestamp') \
      .groupby([pd.TimeGrouper('10min'), 'CAT']) \
      .size().unstack(fill_value=0)
    
    CAT                  200  300  400
    Timestamp                         
    2016-12-02 23:30:00    2    0    0
    2016-12-02 23:40:00    0    1    0
    2016-12-02 23:50:00    0    1    1
    

    【讨论】:

      【解决方案3】:

      IIUC:

      In [246]: df.pivot_table(index='Timestamp', columns='CAT', aggfunc='size', fill_value=0) \
                  .resample('10T').sum()
      Out[246]:
      CAT                  200  300  400
      Timestamp
      2016-12-02 23:30:00    2    0    0
      2016-12-02 23:40:00    0    1    0
      2016-12-02 23:50:00    0    1    1
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-08-16
        • 1970-01-01
        • 1970-01-01
        • 2017-05-19
        • 2021-01-11
        • 1970-01-01
        • 1970-01-01
        • 2020-12-18
        相关资源
        最近更新 更多