从多级索引Pandas DataFrame构建列

时间:2018-10-26 21:30:31

标签: python pandas data-science

我有以下数据框

In [259]: data
Out[259]: 
       campaign  previous    y
0             1         0   no
1             1         0   no
2             1         0   no
3             1         0   no
4             1         0   no
...         ...       ...  ...
45206         3         0  yes
45207         2         0  yes
45208         5         3  yes
45209         4         0   no
45210         2        11   no

[45211 rows x 3 columns]

这是一个市场营销活动数据集,其中:

campaign =当前广告系列(向客户)的致电次数
前一个=上一个广告系列的通话次数
y =响应变量(是=成功)

我需要获取见识,或者...答案:

  • 联系电话(呼叫)与成功与否有关吗? 广告系列?
  • 在前一个广告系列上拨打的电话对该广告系列有影响 成功

为此,我做了:

import pandas as pd
from tabulate import tabulate, tabulate_formats

# Insert column for total calls
tt_contacts = data.campaign+data['previous'] 
data.insert(loc=2, column='tt_contacts', value=tt_contacts)

# Copy for conveniency
data1 = data.copy()

# Remove outliers
q = data1['tt_contacts'].quantile(0.99)
data1 = data1[data1['tt_contacts']<q]

# contingency table of observed counts
ct_total = pd.crosstab(data1.y, data1.tt_contacts)

# column percentages
colsum=ct_total.sum(axis=0)
colsum.astype(int)
colpct=ct_total/colsum

colpct_t = colpct.T.sort_values(by=['yes'], ascending=False)
colpct_t.insert(loc=0, column='Freq.', value=colsum)


In [269]: print(tabulate(colpct_t.head(), tablefmt='fancy_grid',\
headers='keys', numalign="decimal"))
╒═══════════════╤═════════╤══════════╤══════════╕
│   tt_contacts │   Freq. │       no │      yes │
╞═══════════════╪═════════╪══════════╪══════════╡
│             9 │     546 │ 0.842491 │ 0.157509 │
├───────────────┼─────────┼──────────┼──────────┤
│            11 │     321 │ 0.853583 │ 0.146417 │
├───────────────┼─────────┼──────────┼──────────┤
│             5 │    2514 │ 0.863564 │ 0.136436 │
├───────────────┼─────────┼──────────┼──────────┤
│             6 │    1819 │ 0.86641  │ 0.13359  │
├───────────────┼─────────┼──────────┼──────────┤
│             7 │    1099 │ 0.867152 │ 0.132848 │
╘═══════════════╧═════════╧══════════╧══════════╛

此表可以使我相信呼叫次数与广告系列成功有关,如果为真,则最佳呼叫次数为9。 为了加强这一点,我进行了卡方检验,请参见my repo

现在,我想创建一个可视化的图表,显示呼叫对上一个广告系列可能会产生的影响,从而影响当前的成功。

为此,我做了:

In [274]: grouped = pd.DataFrame(data1.groupby(by=\
['tt_contacts','campaign','previous', 'y']).size())

In [275]: grouped
Out[275]: 
                                       0
tt_contacts campaign previous y         
1           1        0        no   12006
                              yes   1523
2           1        1        no    1180
                              yes    351
            2        0        no    9333
...                                  ...
17          8        9        no       6
            9        8        no       1
            12       5        no       1
            17       0        no      63
                              yes      6

[231 rows x 1 columns]

在此df中,tt_contacts的每个频率可以具有1个或多个记录,并具有2个级别,一个级别为“否”,其他级别为“是”。 如何为每个tt_contacts频率的“ yes”和“ no”之和建立可视化,例如:

tt_contacts campaign previous no     yes         
1           1        0        12006  1523
2           1        1         1180   351
...                                   ...
17          8        9           71     6 

0 个答案:

没有答案
相关问题