Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在python数据框架中的列和行之间转换?_Python_Dataframe - Fatal编程技术网

如何在python数据框架中的列和行之间转换?

如何在python数据框架中的列和行之间转换?,python,dataframe,Python,Dataframe,情景1 原始数据帧: a b c d e f g 新数据帧: a b c d e f g 情景2 原始数据帧: g f e d c b a 新数据帧: a b c d e f g 我的问题是如何执行上述转换?请尝试以下操作: import pandas as pd df = pd.DataFrame(columns=list('abcdefg')) df_new = pd.DataFrame([col for col in df.columns], columns=['column_to_ro

情景1

原始数据帧:

a b c d e f g

新数据帧:
a
b
c
d
e
f
g

情景2

原始数据帧:

g f e d c b a

新数据帧:
a
b
c
d
e
f
g

我的问题是如何执行上述转换?

请尝试以下操作:

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)
只需将“.T”添加到数据帧中

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

使用转置方法
df.T
,其中
df
是您的数据帧。