10.3.4 探索性数据分析
在数据集中包含许多分类数据,我们可以进行如下分组和分析:
- 消费者投诉方式(投诉是如何提出的?)
- 消费者向哪家银行投诉(投诉是向哪家银行提出的?)
- 消费者投诉时间线(投诉是何时提出的?)
- 消费者投诉时间线的趋势(数据分组;时间线中是否存在趋势)
- 消费者所在州(投诉发生在哪个州?)
- 银行对消费者投诉的响应(对投诉的响应是什么?)
- 消费者对银行响应的反应(消费者是否对响应提出了异议?)
在接下来的内容中,将对这些分类数据进行分组和分析,以了解数据集中的趋势和模式。
(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)可视化每周的投诉趋势,具体实现流程如下所示:
- 首先,创建了一个名为 complaints 的数据框,它是原始数据 df 的一个副本。
- 然后,根据 'date received' 列对数据进行了分组,计算了每天的投诉数量,结果存储在 complaints_daily 数据框中。
- 接下来,将 complaints_daily 数据重新设置索引,并使用 .resample('W', on='date received').sum() 将数据汇总成每周的投诉数量,结果存储在 complaints_weekly 数据框中。
- 最后,使用 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 每月投诉数量