简体   繁体   English

我如何以某种方式转置熊猫数据框,其中我只需要两列,一列用于标题

[英]How do i transpose a pandas Dataframe in a way, in which I am only gonna need two columns and one column is for the headline

I have a Dataframe which looks something along the lines of this:我有一个数据框,它看起来像这样:

A B C乙丙
1 Data1 Data2 Data3 1 数据1 数据2 数据3
2 Data3 Data1 Nan 2 数据3 数据1 南
3 Nan Data5 Nan 3 楠数据5 楠

And basically i want to get a Dataframe which looks like that:基本上我想得到一个看起来像这样的数据框:

1 1
A Data1一个数据1
A Data3一个数据3
B Data2 B数据2
B Data1 B数据1
B Data5 B数据5
C Data3 C数据3

I already found out that I am probably going to need the transpose function.我已经发现我可能需要转置功能。 But I don't know how to go on from there.但我不知道如何从那里继续。 I would be glad if you could help me out with that or if you find another way which solves this problem.如果您能帮我解决这个问题,或者您找到解决此问题的另一种方法,我会很高兴。

You can do a bit of function chaining:你可以做一些函数链接:

df = df.stack().reset_index(level=0, drop=True).sort_index().dropna()

df.stack() aggregates them in one column but adds a multilevel index so we drop with .reset_index(level=0, drop=True) then we sort the index so it orders the text with sort_index() then finally we dropna() . df.stack()将它们聚合在一列中,但添加了一个多级索引,因此我们使用.reset_index(level=0, drop=True)然后我们对索引进行排序,以便它使用sort_index()对文本进行排序,最后我们dropna() .

Outputs:输出:

A    Data1
A    Data3
B    Data2
B    Data1
B    Data5
C    Data3

If you take off each chained assignment and run them individually you can see what each intermediate step is doing: df.stack() then df.stack().reset_index(level=0, drop=True) , then df.stack().reset_index(level=0, drop=True).sort_index()如果您取消每个链式分配并单独运行它们,您可以看到每个中间步骤在做什么: df.stack()然后df.stack().reset_index(level=0, drop=True) ,然后df.stack().reset_index(level=0, drop=True).sort_index()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将一列的Pandas数据框转换为两列的Pandas数据框? - How do I convert a Pandas Dataframe with one column into a Pandas Dataframe of two columns? 我如何获取两个列表 Pandas DataFrame 列名,并且只使用一个列表,但 append 一个循环中的字符串应用于列名? - How can I take two lists of Pandas DataFrame columns names, and only use one list, but append a string in a loop to be applied to the column name? 如何将数据从 Pandas 数据帧的一列拆分为新数据帧的多列 - How do I split data out from one column of a pandas dataframe into multiple columns of a new dataframe 如何操作我的 pandas dataframe 以每行转置多列? - How do I manipulate my pandas dataframe to transpose multiple columns per row? 在 Pandas 中,将一个数据框中的行转换为另一个数据框中的列的最佳方法? - Optimum way to transpose rows in one dataframe to columns in another dataframe in Pandas? 仅当没有 NaN 时,我如何加入 Pandas dataframe 中两列的值 - How do I join the values of two columns in a Pandas dataframe only if none are NaN 如何将 pandas dataframe 列拆分为 3 个唯一列? - How do I split a pandas dataframe column into 3 unique columns? 如何在Pandas中的数据框中组合两列? - How do I combine two columns within a dataframe in Pandas? 如何在没有索引的情况下转置熊猫中的数据帧? - How do I transpose dataframe in pandas without index? 如何以某种方式转置pandas数据帧? - How do I transpose a pandas dataframe in a certain fashion?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM