簡體   English   中英

Python Pandas:透視數據幀的某些列

[英]Python Pandas: Pivot certain columns of a dataframe

我試圖旋轉一列,但保持周圍的列完好無損。 這是一個示例數據框:

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

描述列已被旋轉且 id 列的值填充值的位置

嘗試后:

df.pivot(index=["index","week], columns="description",values="id"

我得到一個傳遞值長度的錯誤是 x(數據幀的長度),索引意味着 2

注意:您可能需要重置 df 的索引才能訪問數據透視索引。

df.pivot(index=[“index”,”week”], columns=“description”, values=“id”)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM