我有一些看起来像这样的“堆叠”或“记录格式”数据(来自数据库):
"recid","code","value","exam_num"
"101703034","k_rat1","17/18","1"
"200907062","e_mas1","AC YES","6"
"203004134","k_rat1","5/18","5"
"303505091","k_gtrdsc","Foo","1"
"303505091","k_rat1","4/18","2"
我想让它看起来像这样:
recid,exam_num,k_rat1,e_mas1,k_gtrdsc
101703034,1,"17/18",,
200907062,6,,"AC YES",
203004134,5,"5/18",,
303505091,1,,,Foo
303505091,2,"4.18",,
我可以让它只使用一个索引(recid),如下所示:
my_df = read_csv("data.csv")
pivoted = my_df.pivot(index="recid",columns="code",values="value")
这给了我这个(注意缺少的exam_num列):
recid,e_mas1,k_gtrdsc,k_rat1
101703034,,,17/18
200907062,AC YES,,
203004134,,,5/18
303505091,,Foo,4/18
但是,当我尝试指定多个索引或大多数其他索引时,我会遇到各种错误。我读过这个: http: //pandas.pydata.org/pandas-docs/stable/reshaping.html,但看不到一种方法来做我所追求的。
帮助将不胜感激!