可视化数据框中的各行和列之间的多个分类值如何差异
我有以下数据框,其中每列代表索引中这些项目(a,b,…)中的分类算法,
df = pd.DataFrame(index = ['a', 'b', 'c', 'd', 'e', 'f', 'g'])
df['A'] = ['a1', 'a1', 'a2', 'a2', 'a3', 'a4', 'a4']
df['B'] = ['b2', 'b2', 'b2', 'b1', 'b4', 'b3', 'b3']
df['C'] = ['c4', 'c4', 'c4', 'c3', 'c2', 'c2', 'c1']
df:
A B C
a a1 b2 c4
b a1 b2 c4
c a2 b2 c4
d a2 b1 c3
e a3 b4 c2
f a4 b3 c2
g a4 b3 c1
我想在每列中重新排序类别名称,以便我可以更好地评估索引项目是否被类似地分类。跨列。
有没有办法可以看到类别在各列之间的不同?类似于vendiagram。
先感谢您。
I have the following DataFrame where each column represents a categorization algorithm for the items in the index (a,b, …)
df = pd.DataFrame(index = ['a', 'b', 'c', 'd', 'e', 'f', 'g'])
df['A'] = ['a1', 'a1', 'a2', 'a2', 'a3', 'a4', 'a4']
df['B'] = ['b2', 'b2', 'b2', 'b1', 'b4', 'b3', 'b3']
df['C'] = ['c4', 'c4', 'c4', 'c3', 'c2', 'c2', 'c1']
df:
A B C
a a1 b2 c4
b a1 b2 c4
c a2 b2 c4
d a2 b1 c3
e a3 b4 c2
f a4 b3 c2
g a4 b3 c1
I would like to reorder the category names in each column so that I can better assess whether the index items are being categorised similarly across columns.
Is there a way to visualise how the categories differ across columns? Something like a vendiagram.
Thank you in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是我对您有趣的问题的看法。
使用Python标准库 difflib 模块,该模块为计算Deltas提供帮助,您可以定义辅助功能。
一般的想法是使用唯一标识符(基于所有列)对行之间的相似性进行评分,并从最相似的行中对数据框进行排序。
然后,将任意颜色分配给第一行(整体)及其个体值,并通过每一行,并分配上一个颜色(如果相同)或新的颜色(两者都行本身和值),例如,行中的
c2
e
和f
具有相同的颜色。最后,在jupyter笔记本电脑单元格中,运行:
输出:
Here is my take on your interesting question.
Using Python standard library difflib module, which provides helpers for computing deltas, you can define a helper function.
The general idea is to rate similarities between rows using a unique identifier (based on all columns), and sort the dataframe from most similar to less similar rows.
Then, assign an arbitrary color to the first row (as a whole) and its individual values, and go through each row and either assign the previous color (if identical) or a new one (both to row itself and the values), so that, for instance,
c2
in rowse
andf
has the same color.And finally, in a Jupyter notebook cell, run:
Output: