在 pandas 个数据透视表中苦苦挣扎并压平它们

Struggling in pandas pivot tables and flattening them

我正在尝试在 python 中重新创建以下枢轴 table。

在Excel,一切正常。预期为 48 行 x 68 列。这些值是具有特定 row/column.

的项目的“计数”

在 pandas 中,使用相同的数据,我有一个 48 x 962 列的数据透视表 table。此外,我尝试了多种方法来获得扁平化的数据框(没有多索引),但没有成功。

raw csv here

pivot = pd.pivot_table(dataframe, 
                         index = 'customer_IDprovince', 
                         columns = 'category', 
                         aggfunc = len, 
                         fill_value = 0)

此外,我尝试使用枢轴记录获取水平值重命名轴 重置索引 。没有办法让它变平。能帮帮我吗,谢谢 文森佐

使用aggfunc="size"代替len:

pivot = pd.pivot_table(
    df,
    index="customer_IDprovince",
    columns="category",
    aggfunc="size",
    fill_value=0,
)

print(pivot.shape)

打印:

(48, 68)