我试图旋转一列,但使周围的列保持机智。下面是一个数据帧示例:
index id week description
0 1 10-11 failure1
1 2 10-11 failure2
2 2 10-11 failure3
3 2 10-18 failure1
4 3 10-18 failure1
5 3 10-25 failure3
6 3 10-25 failure3
7 4 10-25 failure2
我希望生成的数据帧看起来像这样:
index week failure1 failure2 failure3
0 10-11 1
1 10-11 2
2 10-11 2
3 10-18 2
4 10-18 3
5 10-25 3
6 10-25 3
7 10-25 4
其中description列已被透视,而id列的值填充了这些值
尝试后:
df.pivot(index=["index","week], columns="description",values="id"
我得到一个错误,传递的值的长度是x(数据帧的长度),index意味着2
发布于 2021-10-26 18:52:51
注意:您可能需要重置df的索引才能访问透视的索引。
df.pivot(index=["index","week"], columns="description", values="id")
https://stackoverflow.com/questions/69728444
复制相似问题