我有一个很宽的数据框,我想堆叠和旋转它,但还不太清楚该怎么做.
I have a wide dataframe that I want to stack and pivot and can't quite figure out how to do it.
这就是我要开始的
testdf = pd.DataFrame({"Topic":["A","B","B","C","A"], "Org":[1,1,2,3,5,], "DE1":["a","c","d","e","f"], "DE2":["b","c","a","d","h"], "DE3":["a","c","b","e","f"] }) testdf Out[40]: DE1 DE2 DE3 Org Topic 0 a b a 1 A 1 c c c 1 B 2 d a b 2 B 3 e d e 3 C 4 f h f 5 A我想做的就是旋转表,以便Org的列值是Column名称,每个名称的列值是D1,D2和D3中的匹配值,最后将Topic作为索引.这有可能吗?
What I would like to do is pivot the table so that the column values for Org are the Column names and the column values for each name are the matching values from D1,D2 and D3 and finally have Topic as the index. Is this even possible?
正如Randy C所指出的,如果我使用数据透视,我可以得到以下内容;
As Randy C pointed out, if I use pivot I can get the following;
testdf.pivot(index = "Topic",columns = "Org") Out[44]: DE1 DE2 DE3 Org 1 2 3 5 1 2 3 5 1 2 3 5 Topic A a NaN NaN f b NaN NaN h a NaN NaN f B c d NaN NaN c a NaN NaN c b NaN NaN C NaN NaN e NaN NaN NaN d NaN NaN NaN e NaN哪一个是接近的,但我想拥有它,以便DE值堆叠"而不是宽.结果看起来像;
Which is close, but I would like to have it so that the DE values are "stacked" and not wide. The result would look like;
Org 1 2 3 5 Topic A a NaN NaN f A b NaN NaN h A a NaN NaN f B c d NaN NaN B c a NaN NaN B c b NaN NaN C NaN NaN e NaN C NaN NaN d NaN C NaN NaN e NaN推荐答案
也许:
In[249]: testdf.pivot("Org","Topic").T Out[249]: Org 1 2 3 5 Topic DE1 A a NaN NaN f B c d NaN NaN C NaN NaN e NaN DE2 A b NaN NaN h B c a NaN NaN C NaN NaN d NaN DE3 A a NaN NaN f B c b NaN NaN C NaN NaN e NaN