1

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.

Jan Nehyba
  • 19
  • 3
  • `pandas` `pivot` is probably what you're after https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.pivot.html – Andrew Dec 18 '18 at 08:56
  • Possible duplicate of [How to pivot a dataframe](https://stackoverflow.com/questions/47152691/how-to-pivot-a-dataframe) – Andrew Dec 18 '18 at 08:56
  • 1
    @Andrew this is actually a `melt` problem, the opposite of `pivot`. – yatu Dec 18 '18 at 09:34

5 Answers5

1

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')
yatu
  • 86,083
  • 12
  • 84
  • 139
0
as.vector(unlist(t(df)))

This should do the trick. If you want the result by columns get rid of the t() operator

edit

Sorry I have assumed your columns belong to a dataframe. If this is not the case, you can create one using

df=cbind(x,y,z)
boski
  • 2,437
  • 1
  • 14
  • 30
0

if x, y, z are list object, you can use zip function to retrieve element from these lists simultaneously, and use enumerate function to get the how many index in these lists.

for i, (a, b, c) in enumerate(zip(x, y, z)):
    print("x{} {}".format(i, a))
    print("y{} {}".format(i, b))
    print("z{} {}".format(i, c))
Lester_wu
  • 151
  • 5
0

First stack the columns and then drop the multiindex:

df2 = df.stack().reset_index(drop=True)

Now sort the values

print df2.sort_values().reset_index(drop=True)

After this you can name the column.

LOrD_ARaGOrN
  • 3,884
  • 3
  • 27
  • 49
0
x <- c(0.2, 0.75, 0.78, 0.56, 0.45, 0.2)
y <- c(0.5, 0.58, 0.86, 0.75, 0.47, 0.58)
z <- c(0.26, 0.25, 0.95, 0.52, 0.57, 0.98)

d <- data.frame(x,y,z)


datalist <- list()
for( i in 1:nrow(d))
{
  m <- d[i,]
  datalist[[i]] <- m
}
output <- unlist(datalist,use.names = F)
output

output

0.20 0.50 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.20
0.58 0.98

Hope this will work for you.

Regards, Dinesh Vasu

massisenergy
  • 1,764
  • 3
  • 14
  • 25
Dinesh
  • 56
  • 5