如何绘制堆积条形图以汇总每个分类列的值比例
How to plot stacked bar chart to summarise each categorical column for proportion of values
我有这样一个数据框:
user_id action action_type action_detail device_type secs_elapsed
0 d1mm9tcy42 lookup Missing Missing Windows Desktop 319
1 d1mm9tcy42 search_results click view_search_results Windows Desktop 67753
2 d1mm9tcy42 lookup Missing Missing Windows Desktop 301
3 d1mm9tcy42 search_results click view_search_results Windows Desktop 22141
4 d1mm9tcy42 lookup Missing Missing Windows Desktop 435
5 d1mm9tcy42 search_results click view_search_results Windows Desktop 7703
6 d1mm9tcy42 lookup Missing Missing Windows Desktop 115
7 d1mm9tcy42 personalize data wishlist_content_update Windows Desktop 831
8 d1mm9tcy42 index view view_search_results Windows Desktop 20842
9 d1mm9tcy42 lookup Missing Missing Windows Desktop 683
我想设置一个在 x 轴上有分类列的条形图,例如action
、action_type
和 action_detail
,以及在 y 轴上具有值 Missing
、[=16= 的行数的百分比计数(对于每列) ](你在这里看不到这个,但有些列确实有那个值)和 Other
(任何不是 Missing
或 Unknown
的东西)。
我苦苦挣扎的一件事也是如何查看 action
列中的每个值,action_type
和 action_detail
分别丢失的百分比是多少或未知。例如动作 lookup
发生了 100 次,其中 20% 的时间是 Missing
action_type
等
我已经通过这种类型的代码找到了答案:
print("The percentage of missing action types is {0}".format
(((clean_sessions['action_type'] == 'Missing').value_counts())/(clean_sessions['action_type'].count())
))
但我想将我的分析提升到一个新的水平。
- 删除不相关的列。
- 使所有值都在
('Missing', 'Unknown', 'Other')
. 中
- 在每列上调用
value_counts
。
- 当值不在列中时,计数将是
nan
而不是 0
,因此您可能希望在末尾使用 fillna(0)
。
- 您已经有了所需的数据,只需绘制即可。
-
result = (df[['action', 'action_type', 'action_detail']]
.where(df.isin(('Missing', 'Unknown')), 'Other')
.apply(lambda x: x.value_counts(normalize=True))
.fillna(0))
print(result)
action action_type action_detail
Missing 0 0.5 0.5
Other 1 0.5 0.5
result.T.plot(kind='bar', stacked=True)
我有这样一个数据框:
user_id action action_type action_detail device_type secs_elapsed
0 d1mm9tcy42 lookup Missing Missing Windows Desktop 319
1 d1mm9tcy42 search_results click view_search_results Windows Desktop 67753
2 d1mm9tcy42 lookup Missing Missing Windows Desktop 301
3 d1mm9tcy42 search_results click view_search_results Windows Desktop 22141
4 d1mm9tcy42 lookup Missing Missing Windows Desktop 435
5 d1mm9tcy42 search_results click view_search_results Windows Desktop 7703
6 d1mm9tcy42 lookup Missing Missing Windows Desktop 115
7 d1mm9tcy42 personalize data wishlist_content_update Windows Desktop 831
8 d1mm9tcy42 index view view_search_results Windows Desktop 20842
9 d1mm9tcy42 lookup Missing Missing Windows Desktop 683
我想设置一个在 x 轴上有分类列的条形图,例如action
、action_type
和 action_detail
,以及在 y 轴上具有值 Missing
、[=16= 的行数的百分比计数(对于每列) ](你在这里看不到这个,但有些列确实有那个值)和 Other
(任何不是 Missing
或 Unknown
的东西)。
我苦苦挣扎的一件事也是如何查看 action
列中的每个值,action_type
和 action_detail
分别丢失的百分比是多少或未知。例如动作 lookup
发生了 100 次,其中 20% 的时间是 Missing
action_type
等
我已经通过这种类型的代码找到了答案:
print("The percentage of missing action types is {0}".format
(((clean_sessions['action_type'] == 'Missing').value_counts())/(clean_sessions['action_type'].count())
))
但我想将我的分析提升到一个新的水平。
- 删除不相关的列。
- 使所有值都在
('Missing', 'Unknown', 'Other')
. 中
- 在每列上调用
value_counts
。 - 当值不在列中时,计数将是
nan
而不是0
,因此您可能希望在末尾使用fillna(0)
。 - 您已经有了所需的数据,只需绘制即可。
-
result = (df[['action', 'action_type', 'action_detail']]
.where(df.isin(('Missing', 'Unknown')), 'Other')
.apply(lambda x: x.value_counts(normalize=True))
.fillna(0))
print(result)
action action_type action_detail
Missing 0 0.5 0.5
Other 1 0.5 0.5
result.T.plot(kind='bar', stacked=True)