(10-3-04-01)银行消费者投诉处理模型

本文介绍了如何通过Python中的Pandas和PlotlyExpress库对数据集中的分类数据进行分组和分析,包括消费者投诉方式、银行、时间线趋势、地理位置以及银行响应等。通过实例展示了如何使用value_counts、plot_subset_counts函数创建图表来洞察数据集中的模式和趋势,如投诉分布、银行频率和时间序列分析。
摘要由CSDN通过智能技术生成

10.3.4  探索性数据分析

在数据集中包含许多分类数据,我们可以进行如下分组和分析:

  1. 消费者投诉方式(投诉是如何提出的?)
  2. 消费者向哪家银行投诉(投诉是向哪家银行提出的?)
  3. 消费者投诉时间线(投诉是何时提出的?)
  4. 消费者投诉时间线的趋势(数据分组;时间线中是否存在趋势)
  5. 消费者所在州(投诉发生在哪个州?)
  6. 银行对消费者投诉的响应(对投诉的响应是什么?)
  7. 消费者对银行响应的反应(消费者是否对响应提出了异议?)

在接下来的内容中,将对这些分类数据进行分组和分析,以了解数据集中的趋势和模式。

1使用value_counts(ascending=True)查看 'submitted via' 列中不同提交方式的计数,按升序排列。这将返回一个包含每种提交方式计数的 Series 对象,计数从最低到最高排列。具体实现代码如下所示。

df['submitted via'].value_counts(ascending=True)

执行后输出:

Web    184012

Name: submitted via, dtype: int64

(2)定义一个名为 plot_subset_counts 的函数,该函数用于绘制数据框中某一列的子集计数。然后调用函数 plot_subset_counts绘制了一个垂直条形图,显示了前 10 个银行的计数。这有助于可视化数据集中不同银行的出现频率。具体实现代码如下所示。

def plot_subset_counts(df,column,orient='h',top=None):  
    ldf = df[column].value_counts(ascending=False).to_frame()
    ldf.columns = ['values']  
    if(top):
        ldf = ldf[:top]    
    if(orient is 'h'):
        fig = px.bar(data_frame=ldf,
                     x = ldf.index,
                     y = 'values',
                     template='plotly_white',
                     title='Subset Value-Counts')
    elif('v'):

        fig = px.bar(data_frame=ldf,
                             y = ldf.index,
                             x = 'values',
                             template='plotly_white',
                             title='Subset Value-Counts')
        
    fig.update_layout(height=400)
    fig.update_traces(marker_line_color='white',
                      marker_line_width=0.5,
                      marker={'color':'#F1A424'},
                      width=0.75)
    
    fig.show("png")
plot_subset_counts(df,'company',orient='v',top=10)

执行效果如图10-26所示

10-26  前十个银行的数量统计图

3计算了 'company public response' 列中不同银行公共响应的计数,并将结果转换为 DataFrame。然后,使用 .style.bar() 方法为该 DataFrame 创建了一个样式,包括设置了条形图的样式、颜色和对齐方式。具体实现代码如下所示。

ldf = df['company public response'].value_counts(ascending=False).to_frame()
ldf.style\
    .bar(align='mid',
         color=['#3b3745','#F1A424'])

执行效果如图10-27所示,这个操作可用于可视化不同银行的公共响应计数,并使用不同的颜色来区分条形。

10-27  不同银行公共响应的计数

(4)可视化每周的投诉趋势,具体实现流程如下所示:

  1. 首先,创建了一个名为 complaints 的数据框,它是原始数据 df 的一个副本。
  2. 然后,根据 'date received' 列对数据进行了分组,计算了每天的投诉数量,结果存储在 complaints_daily 数据框中。
  3. 接下来,将 complaints_daily 数据重新设置索引,并使用 .resample('W', on='date received').sum() 将数据汇总成每周的投诉数量,结果存储在 complaints_weekly 数据框中。
  4. 最后,使用 Plotly Express 创建了一条线图,显示了每周的投诉数量,图表的标题为 "Weekly Complaints",并将图表以 PNG 格式显示出来。

具体实现代码如下所示。

complaints = df.copy()
complaints_daily = complaints.groupby(['date received']).agg("count")[["product"]] # daily addresses

# Sample weekly
complaints_weekly = complaints_daily.reset_index()
complaints_weekly = complaints_weekly.resample('W', on='date received').sum() # weekly addresses

fig = px.line(complaints_weekly,complaints_weekly.index,y="product",
              template="plotly_white",title="Weekly Complaints",height=400)
fig.update_traces(line_color='#F1A424')
fig.show("png")

执行效果如图10-28所示,这有助于理解投诉的季节性变化或趋势。

10-28  执行效果

5使用 Plotly Express 创建一个柱状图,显示了每月的投诉趋势,具体来说,它显示了每天的投诉数量。具体实现代码如下所示。

fig = px.bar(df['day'].value_counts(ascending=True).to_frame(),y='day',
             template='plotly_white',height=300,
             title='Day of the Month Complaint Trends')
fig.update_xaxes(tickvals = [i for i in range(0,32,1)])
fig.update_traces(marker_line_color='#F1A424',marker_line_width=0.1,
                  marker={'color':'#F1A424'},width=0.5)
fig.update_traces(textfont_size=12, textangle=0, 
                  textposition="outside", cliponaxis=False)
fig.show("png")

执行后使用 fig.show("png") 显示生成的柱状图,效果如图10-29所示,可用于可视化每天的投诉趋势。

10-29  每天投诉数量

6使用 Plotly Express 创建一个柱状图,显示了每个月的投诉数量。具体实现代码如下所示。

fig = px.bar(df['month'].value_counts(ascending=False).to_frame(),y='month',
             template='plotly_white',height=300,
             title='Month of the Year Complaint Trends')
fig.update_xaxes(tickvals = [i for i in range(0,13,1)])
fig.update_traces(marker_line_color='#F1A424',marker_line_width=0.1,
                  marker={'color':'#F1A424'},width=0.5)
fig.show("png")

执行效果如图10-30所示,用于可视化每个月的投诉趋势。

10-30  每月投诉数量

  • 20
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

码农三叔

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值