在Python中堆叠和旋转数据框

编程入门 行业动态 更新时间:2024-10-11 15:22:33
本文介绍了在Python中堆叠和旋转数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个很宽的数据框,我想堆叠和旋转它,但还不太清楚该怎么做.

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

更多推荐

在Python中堆叠和旋转数据框

本文发布于:2023-11-30 07:01:31,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1649103.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据   Python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!