Bschieland
Bschieland

Reputation: 23

pd transpose multiple rows into single column

Dear friends i want to transpose the following dataframe into a single column. I cant figure out a way to transform it so your help is welcome!! I tried pivottable but sofar no succes

X   0.00    1.25    1.75    2.25    2.99    3.25    
X   3.99    4.50    4.75    5.25    5.50    6.00    
X   6.25    6.50    6.75    7.50    8.24    9.00    
X   9.50    9.75    10.25   10.50   10.75   11.25    
X   11.50   11.75   12.00   12.25   12.49   12.75    
X   13.25   13.99   14.25   14.49   14.99   15.50

and it should look like this

X    
0.00    
1.25    
1.75    
2.25    
2.99    
3.25    
3.99    
4.5    
4.75    
5.25    
5.50    
6.00    
6.25

etc..

Upvotes: 1

Views: 646

Answers (2)

Bschieland
Bschieland

Reputation: 23

ty so much!! A follow up question(a)

Is it also possible to stack the df into 2 columns X and Y this is the data set

This is the data set. 1 2 3 4 5 6 7

X 0.00 1.25 1.75 2.25 2.99 3.25

Y -1.08 -1.07 -1.07 -1.00 -0.81 -0.73

X 3.99 4.50 4.75 5.25 5.50 6.00

Y -0.37 -0.20 -0.15 -0.17 -0.15 -0.16

X 6.25 6.50 6.75 7.50 8.24 9.00

Y -0.17 -0.18 -0.24 -0.58 -0.93 -1.24

X 9.50 9.75 10.25 10.50 10.75 11.25

Y -1.38 -1.42 -1.51 -1.57 -1.64 -1.75

X 11.50 11.75 12.00 12.25 12.49 12.75

Y -1.89 -2.00 -2.00 -2.04 -2.04 -2.10

X 13.25 13.99 14.25 14.49 14.99 15.50

Y -2.08 -2.13 -2.18 -2.18 -2.27 -2.46

Upvotes: 0

zipa
zipa

Reputation: 27879

This will do it, df.columns[0] is used as I don't know what are your headers:

df = pd.DataFrame({'X': df.set_index(df.columns[0]).stack().reset_index(drop=True)})

df

        X
0    0.00
1    1.25
2    1.75
3    2.25
4    2.99
5    3.25
6    3.99
7    4.50
8    4.75
9    5.25
10   5.50
11   6.00
12   6.25
13   6.50
14   6.75
15   7.50
16   8.24
17   9.00
18   9.50
19   9.75
20  10.25
21  10.50
22  10.75
23  11.25
24  11.50
25  11.75
26  12.00
27  12.25
28  12.49
29  12.75
30  13.25
31  13.99
32  14.25
33  14.49
34  14.99
35  15.50

Upvotes: 1

Related Questions