Could you give me an advice, please?
I have three columns, e.x.:
x y z
0.2 0.5 0.26
0.75 0.58 0.25
0.78 0.86 0.95
0.56 0.75 0.52
0.45 0.47 0.57
0.2 0.58 0.98
Result: And I need only one column with this sequence:
xyz
x1 0.2
y1 0.5
z1 0.26
x2 0.75
y2 0.58
z2 0.25
... 0.78
0.86
0.95
…
I didn´t find a similar problem... Thank You very much. I tried to use "dcast" (R) or "concat" (Python) but I am stack.
You can use pd.melt
and create the row names you want with np.tile
:
m = df.T.melt(value_name='xyz')
rows = np.tile(df.columns, df.shape[0])
m['variable'] = rows.tolist() + (m.variable + 1).astype(str)
variable xyz
0 x1 0.20
1 y1 0.50
2 z1 0.26
3 x2 0.75
4 y2 0.58
5 z2 0.25
6 x3 0.78
7 y3 0.86
8 z3 0.95
9 x4 0.56
10 y4 0.75
11 z4 0.52
12 x5 0.45
13 y5 0.47
14 z5 0.57
15 x6 0.20
16 y6 0.58
17 z6 0.98
And if you want is as index do:
m.set_index('variable')