简体   繁体   English

如何在 python dataframe 中的列和行之间进行转换?

[英]How to convert between columns and rows in python dataframe?

scenario 1情景 1

original dataframe:原装 dataframe:

ab c defg ab c defg

new dataframe:新 dataframe:
a一个
b b
c c
d d
e e
f F
g G

scenario 2情景 2

original dataframe:原装 dataframe:

gfed c ba gfed c 巴

new dataframe:新 dataframe:
a一个
b b
c c
d d
e e
f F
g G

My question is how to perform the above transformations?我的问题是如何执行上述转换?

Try the following:尝试以下操作:

import pandas as pd

df = pd.DataFrame(columns=list('abcdefg'))
df_new = pd.DataFrame([col for col in df.columns], columns=['column_to_rows'])

print(df_new)

just add ".T" to your dataframe.只需将“.T”添加到您的 dataframe 即可。

pd.DataFrame(['a','b','c', 'd', 'e', 'f', 'g'])
Out[99]: 
   0
0  a
1  b
2  c
3  d
4  e
5  f
6  g

pd.DataFrame(['a','b','c', 'd', 'e', 'f', 'g']).T

Out[100]: 
   0  1  2  3  4  5  6
0  a  b  c  d  e  f  g

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM