【问题标题】:creating histograms in pandas在熊猫中创建直方图
【发布时间】:2015-12-09 05:40:22
【问题描述】:

我正在尝试根据以下 groupby 创建直方图,

dfm.groupby(['ID', 'Readings', 'Condition']).size:
578871001  20110603         True    1
           20110701         True    1
           20110803         True    1
           20110901         True    1
           20110930         True    1
                                          ..
324461897  20130214         False            1
           20130318         False            1
           20130416         False            1
           20130516         False            1
           20130617         False            1
532674350  20110616         False            1
           20110718         False            1
           20110818         False            1
           20110916         False            1
           20111017         False            1
           20111115         False            1
           20111219         False            1

但是,我正在尝试按Condition 格式化输出并将IDReadings 的数量分组。像这样的,

True
 # of Readings: # of ID
  1 : 5
  2 : 8
  3 : 15
  4 : 10
  5 : 4

我尝试过仅按 ID 和 Readings 分组,并按 Condition 进行转换,但还没有走得很远。

编辑:

这是 groupby 之前数据框的样子:

         CustID     Condtion      Month          Reading  Consumption
0     108000601         True       June         20110606      28320.0
1     108007000         True       July         20110705      13760.0
2     108007000         True     August         20110804      16240.0
3     108008000         True  September         20110901      12560.0
4     108008000         True    October         20111004      12400.0
5     108000601        False   November         20111101       9440.0
6     108090000        False   December         20111205      12160.0

【问题讨论】:

  • 能否在分组前附上dfm 数据框?

标签: python pandas histogram


【解决方案1】:

这是您想要通过groupby 实现的目标吗?我已经包含Counter 来跟踪每个读数的计数。例如,对于 Condtion = False,有两个 CustID 有一个读数,所以第一行的输出是:

Condtion
False   1  2  # One reading, two observations of one reading.

然后,对于 Condtion = True,有一个客户有一个读数 (108000601) 和两个客户每个有两个读数。该组的输出是:

Condtion
True   1  1  # One customer with one reading.
       2  2  # Two customers with two readings each.


from collections import Counter

gb = df.groupby(['Condtion', 'CustID'], as_index=False).Reading.count()
>>> gb
  Condtion     CustID  Reading
0    False  108000601        1
1    False  108090000        1
2     True  108000601        1
3     True  108007000        2
4     True  108008000        2

>>> gb.groupby('Condtion').Reading.apply(lambda group: Counter(group))

Condtion   
False     1    2
True      1    1
          2    2
dtype: float64

或者,作为一条语句链接在一起:

gb = (df
      .groupby(['Condtion', 'CustID'], as_index=False)['Reading']
      .count()
      .groupby('Condtion')['Reading']
      .apply(lambda group: Counter(group))
)

【讨论】:

  • 不完全。它应该是 Number of Readings 和 Number of ID's,根据 Condition 具有相同的读数数量。
  • 好的。但是您的示例数据只有一个 CustID。当前表提供了每个 CustID 的读数数量,因此您想要按 Condition 获取这些计数的计数吗?
  • 哎呀。我已经修复了示例数据。
  • 是的。做到了。我以错误的方式进行操作,并没有意识到我可以像这样将 Reading 链接到 groupby。谢谢!
猜你喜欢
  • 1970-01-01
  • 2018-07-05
  • 2018-12-05
  • 1970-01-01
  • 2014-08-26
  • 2017-09-24
  • 2020-10-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多