我有一个熊猫数据框,格式为:
colA | colB | counts
car1 plane1 23
car2 plane2 51
car1 plane2 12
car2 plane3 41
我首先要创建一个看起来有点像矩阵(similar to the df in this example)的熊猫数据框,还要用0填充缺失值。因此,上述的理想结果将是:
plane1 plane2 plane3
car1 23 12 0
car2 0 51 41
然后可以将其转换为热图。我可以使用一个熊猫命令吗?
答案 0 :(得分:0)
pandas.pivot_table
转换数据,seaborn.heatmap
创建热图
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
piv = pd.pivot_table(df, index='colA', columns='colB', aggfunc='sum', fill_value=0)
piv.columns = piv.columns.droplevel(0)
sns.heatmap(piv)
plt.show()