Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.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 3.x 一次转置一列的范围-1_Python 3.x_Pandas_Transpose - Fatal编程技术网

Python 3.x 一次转置一列的范围-1

Python 3.x 一次转置一列的范围-1,python-3.x,pandas,transpose,Python 3.x,Pandas,Transpose,我有一个这样的数据框 id pd pd_dt pd_tp pd.1 pd_dt.1 pd_tp.1 pd.2 pd_dt.2 pd_tp.2 0 1 100 per year 468 200 per year 400 300 per year 320 1 2 100 per year 60 200 per year 890 300

我有一个这样的数据框

  id    pd  pd_dt      pd_tp    pd.1    pd_dt.1     pd_tp.1 pd.2    pd_dt.2     pd_tp.2
0  1    100 per year   468      200     per year    400     300     per year    320
1  2    100 per year   60       200     per year    890     300     per year    855
id Transposed_value
1  100
1  per year
1  468
1  200
1  per year
1  400
1  300
1  per year
1  320
2  100
2  per year
2  360
2  200
2  per year
2  890
2  300
2  per year
2  855
现在,我需要这样的输出

  id    pd  pd_dt      pd_tp    pd.1    pd_dt.1     pd_tp.1 pd.2    pd_dt.2     pd_tp.2
0  1    100 per year   468      200     per year    400     300     per year    320
1  2    100 per year   60       200     per year    890     300     per year    855
id Transposed_value
1  100
1  per year
1  468
1  200
1  per year
1  400
1  300
1  per year
1  320
2  100
2  per year
2  360
2  200
2  per year
2  890
2  300
2  per year
2  855
如果您看到,
pd
pd\u dt
pd\u tp
正在使用
pd.1
重复

我试过了

df.T

这并没有给我想要的输出。任何有助于实现我的结果的建议或帮助都将非常好。

使用:

df.set_index('id').stack().reset_index(name='Transposed_value').drop('level_1',axis=1)
输出

   id   Transposed_value
0   1   100
1   1   per year
2   1   468
3   1   200
4   1   per year
5   1   400
6   1   300
7   1   per year
8   1   320
9   2   100
10  2   per year
11  2   60
12  2   200
13  2   per year
14  2   890
15  2   300
16  2   per year
17  2   855
使用:

输出

   id   Transposed_value
0   1   100
1   1   per year
2   1   468
3   1   200
4   1   per year
5   1   400
6   1   300
7   1   per year
8   1   320
9   2   100
10  2   per year
11  2   60
12  2   200
13  2   per year
14  2   890
15  2   300
16  2   per year
17  2   855