【发布时间】:2022-01-08 17:31:55
【问题描述】:
我有一个类似的 DataFrame
event entity type
1 T1 Table
1 C1 Cell
1 C2 Cell
2 T2 Table
2 C3 Cell
我想把它转换成这样的东西
event Table Cell
1 T1 C1
1 T1 C2
2 T2 C3
这是 pd.melt 和 drop_duplicates() 的输出,我读到 Melt 的对立面是枢轴,因此尝试以这种方式处理单个事件。
df.pivot_table(values='entity', index='event', columns='type')
但这会引发DataError: No numeric types to aggregate 的错误,这很明显,因为 pivot 会按聚合进行分组,并且不会复制每个组合。知道这种操作到底可以做什么吗?请注意我为了简化而丢弃的 groupby 东西。
【问题讨论】:
标签: python python-3.x pandas pivot melt