Pandas 从带有复选标记的 CSV 数据透视 table
Pandas pivot table from CSV with checkmarks
我有一个看起来像这样的 CSV 文件
data_source
destination
description
A
1
A description
B
2
B description
C
3
C description
A
3
A description
我希望生成一个看起来像这样的枢轴 table
description
data_source
1
2
3
A description
A
x
x
B description
B
x
C description
C
x
到目前为止我得到的最接近的是这个
table = pd.pivot_table(df, index='data_source', columns='destination', values=['data_source'], aggfunc='first')
我尝试使用 ['description'、'data_source'] 作为索引来获取描述和 data_source 行,但我最终得到了 0 列 table。我也不知道传递给 aggfunc 以在有价值的单元格中输出 'x' 的数据类型。
我是 Pandas 的新手,我还没有一个好的心智模型,所以非常感谢您的帮助。
您正在寻找pd.crosstab
out = pd.crosstab([df['data_source'],df['description']], df['destination']).reset_index()
Out[101]:
destination data_source description 1 2 3
0 A Adescription 1 0 1
1 B Bdescription 0 1 0
2 C Cdescription 0 0 1
我有一个看起来像这样的 CSV 文件
data_source | destination | description |
---|---|---|
A | 1 | A description |
B | 2 | B description |
C | 3 | C description |
A | 3 | A description |
我希望生成一个看起来像这样的枢轴 table
description | data_source | 1 | 2 | 3 |
---|---|---|---|---|
A description | A | x | x | |
B description | B | x | ||
C description | C | x |
到目前为止我得到的最接近的是这个
table = pd.pivot_table(df, index='data_source', columns='destination', values=['data_source'], aggfunc='first')
我尝试使用 ['description'、'data_source'] 作为索引来获取描述和 data_source 行,但我最终得到了 0 列 table。我也不知道传递给 aggfunc 以在有价值的单元格中输出 'x' 的数据类型。
我是 Pandas 的新手,我还没有一个好的心智模型,所以非常感谢您的帮助。
您正在寻找pd.crosstab
out = pd.crosstab([df['data_source'],df['description']], df['destination']).reset_index()
Out[101]:
destination data_source description 1 2 3
0 A Adescription 1 0 1
1 B Bdescription 0 1 0
2 C Cdescription 0 0 1