带间隙的 Seaborn 热图

Seaborn heat map with gaps

我正在使用 dataset from kaggle 并尝试对其进行一些数据分析。

首先我计算了每组品牌和车型的平均价格 (This is my average code) and after that, I did the heat map from this average (Heat map code)(Heat map figure)。但是,注意到在数据集中某些品牌没有所有车辆类型,例如 alfa_romeo 不显示 "bus" 类型。这成为一个问题,因为这种缺失在热图中显示为空白。

如何克服这种情况,例如,将零值放在有差距的地方?

尝试将参数 fill_value = 0 添加到 Heat map code 中的 df.pivot。这应将 NULL 值替换为 0 并防止在热图中显示间隙。

编辑:我的解决方案出错,因为 pandas.DataFrame.pivot 没有 fill_value 的参数。更好的选择是 pandas.pivot_table,它或多或少等同于 pandas.pivot,但具有更大的灵活性。看这里:https://pandas.pydata.org/pandas-docs/stable/generated/pandas.pivot_table.html

下面是你的行应该如何重写:

import pandas as pd
df2_pivot = pd.pivot_table(data = df2, 
                           index = 'brand', 
                           columns = 'vehicleType', 
                           values = 'avgPrice', 
                           fill_value = 0)

或者,您也可以 运行:

df2_pivot = df2.pivot(index = 'brand', 
                      columns = 'vehicleType', 
                      values = 'avgPrice').fillna(0)