Fil*_*tio 4 python plot pandas
我有以下三个变量的数据集:
- df['Score'] 浮动虚拟(1 或 0)
- df['Province'] 一个对象列,其中每一行是一个区域
- df['Product type'] 指示行业的对象。
我想创建一个联合图,在 x 轴上我有不同的行业,在 y 轴上是不同的省份,作为我的联合图的颜色,我有得分的相对频率。像这样的东西。 https://seaborn.pydata.org/examples/hexbin_marginals.html
暂时我只能做到以下几点
mean = df.groupby(['Province', 'Product type'])['score'].mean()
Run Code Online (Sandbox Code Playgroud)
但我不确定如何绘制它。
谢谢!
小智 7
如果您正在寻找热图,您可以使用 seabornheatmap函数。但是,您需要先旋转您的桌子。
只是创建一个小例子:
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
score = [1, 1, 1, 0, 1, 0, 0, 0]
provinces = ['Place1' ,'Place2' ,'Place2', 'Place3','Place1', 'Place2','Place3','Place1']
products = ['Product1' ,'Product3' ,'Product2', 'Product2','Product1', 'Product2','Product1','Product1']
df = pd.DataFrame({'Province': provinces,
'Product type': products,
'score': score
})
Run Code Online (Sandbox Code Playgroud)
我的df样子:
'Province''Product type''score'
0 Place1 Product1 1
1 Place2 Product3 1
2 Place2 Product2 1
3 Place3 Product2 0
4 Place1 Product1 1
5 Place2 Product2 0
6 Place3 Product1 0
7 Place1 Product1 0
Run Code Online (Sandbox Code Playgroud)
然后:
df_heatmap = df.pivot_table(values='score',index='Province',columns='Product type',aggfunc=np.mean)
sns.heatmap(df_heatmap,annot=True)
plt.show()
Run Code Online (Sandbox Code Playgroud)
结果是:

| 归档时间: |
|
| 查看次数: |
5609 次 |
| 最近记录: |