如何创建一个直方图,显示相对于另一个变量的指定范围的二项式值的计数

How to create a histogram that displays counts of binomial values with respect to a specified range of another variable

很难用一句话说清楚我在找什么,所以我在这里提供一个更清楚的解释:

概述:我有一个单元格数据集 phone 客户数据和两列可供使用;流失和服务中断,我想创建一个直方图,根据总服务中断时间显示已流失的客户数量。我将服务中断时间分组到范围内,除了他们的中断时间之外,每个客户都有一个是的值或没有值。下面是图表在 Excel 中的样子 - 包括 10,000 个点的整个数据集,这就是计数要大得多的原因。图表的外观正是我想要的。

目标:能够在 Python.

中做到这一点

问题:导入数据后我处理了一些问题。我将 Yes/No 值转换为 1 和 0,并且已经能够创建一个 groupby 数据框来输出指定范围内停电的客户数量,如下所示。

import pandas
import numpy

# create DF    
df = pandas.DataFrame({
'Churn':
    ['Yes', 'Yes', 'No', 'Yes', 'No', 'Yes', 'No', 'No', 'Yes', 'No'],
'Outage_sec_perweek':
    [10.964310, 12.292789, 10.923990, 14.960431, 8.131345, 7.554437, 9.366187, 9.879618, 9.509801, 10.379130]})


df['Churn'] = df['Churn'].apply(lambda x: 1 if x == 'Yes' else 0)
# Which outputs
   Churn  Outage_sec_perweek
0      0            7.978323
1      1           11.699080
2      0           10.752800
3      0           14.913540
4      1            8.147417
5      0            8.420993
6      1           11.182725
7      1            7.791632
8      0            5.739006
9      0            8.707824

df1 = df.groupby(pandas.cut(df['Outage_sec_perweek'], numpy.arange(0, 
df['Outage_sec_perweek'].max() + 5, 5))).count()
print(df1)

# Which outputs - Outage column is correct but the churn column is not
                    Churn  Outage_sec_perweek
Outage_sec_perweek                           
(0.0, 5.0]              0                  0
(5.0, 10.0]             6                  6
(10.0, 15.0]            4                  4

显然这个结果不正确或不理想,因为我没有通过流失与非流失来区分流失列。

缺少的部分有一个 1 和 0 的计数与每个中断计数相关联,因此生成的数据帧将类似于:

                    Outage_sec_perweek    No_Churn   Yes_Churn
Outage_sec_perweek                    
(0.0, 5.0]                           0       0           0
(5.0, 10.0]                          6       4           2
(10.0, 15.0]                         4       2           2 

当然,我们的目标是将 numpy 创建的范围应用于流失与无流失的计数过程。我知道如何计算流失和未流失的人数,但根据他们经历的中断时间对他们进行分组,这是我以前从未应用过的东西——根据 Python 和 pandas.我不想求助于制定冗长的条件,例如: df = df.loc[(df['Outage_sec_perweek'] >= 0) & (df['Outage_sec_perweek'] < 5)]...

等等。

设置

print(df)
   Churn  Outage_sec_perweek
0      0            7.978323
1      1           11.699080
2      0           10.752800
3      0           14.913540
4      1            8.147417
5      0            8.420993
6      1           11.182725
7      1            7.791632
8      0            5.739006
9      0            8.707824

首先将列 Outage_sec_perweek 分类为离散间隔,然后使用 crosstab 创建一个频率 table,它显示特定中断间隔内的流失计数。然后使用 plot 方法创建一个漂亮的条形图显示分布

s = df['Outage_sec_perweek']
s = pd.cut(s, bins=np.r_[0 : s.max() + 5 : 5])

table = pd.crosstab(s, df['Churn'])\
          .reindex(s.cat.categories, fill_value=0)

#OR table = df.groupby([s, 'Churn'])['Outage_sec_perweek'].count().unstack()

频率输出table

print(table)

Churn               0  1
Outage_sec_perweek      
(0.0, 5.0]          0  0
(5.0, 10.0]         4  2
(10.0, 15.0]        2  2

条形图的输出

table.plot(kind='bar')