Python 在熊猫中创建直方图
我试图根据下面的groupby创建一个直方图Python 在熊猫中创建直方图,python,pandas,histogram,Python,Pandas,Histogram,我试图根据下面的groupby创建一个直方图 dfm.groupby(['ID', 'Readings', 'Condition']).size: 578871001 20110603 True 1 20110701 True 1 20110803 True 1 20110901 True 1 20110930
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
但是,我正在尝试按条件
格式化输出,并将ID
和读数的数量分组。像这样的,
True
# of Readings: # of ID
1 : 5
2 : 8
3 : 15
4 : 10
5 : 4
我试过只根据ID和读数进行分组,并根据条件进行转换,但还没走多远
编辑:
这是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
这就是您试图通过groupby
实现的目标吗?我加入了计数器
来跟踪每次读数的计数。例如,对于condition=False,有两个custid具有单个读取,因此第一行的输出为:
Condtion
False 1 2 # One reading, two observations of one reading.
然后,对于condition=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))
)
在分组之前,您可以附加您的dfm
数据帧吗?不完全可以。它应该是读数的数量和ID的数量,根据条件具有相同的读数数量。OK。但是您的示例数据只有一个CustID。表current提供了每个CustID的读数数,因此您需要按条件?oops获取这些计数的计数。我已经修正了样本数据。是的。成功了。我走错了方向,没有意识到我可以像那样把阅读链接到一个群组。谢谢