0

I have a data frame that has the columns x, y, z, x2, y2, z2.

What I would like to do is weave the columns together so they look like:

x  y  z
x2 y2 z2

For example:

x   y   z   x2  y2  z2
1   1   1   2   2   2
2   2   2   3   3   3
3   3   3   4   4   4

I want it to look like this:

x   y   z
1   1   1
2   2   2
2   2   2
3   3   3
3   3   3
4   4   4

Is there a way to accomplish this with the zip function?

cccnrc
  • 1,195
  • 11
  • 27
  • Do you want to combine, say both `x` and `x2` and then sort their values? – Sam Chats Aug 15 '19 at 14:19
  • def to_array(dataframe): weaved = np.empty((len(dataframe) * 2, 3)) weaved[0::2] = dataframe[['x', 'y', 'z']].values weaved[1::2] = dataframe[['x2', 'y2', 'z2']].values return weaved # I've accomplished this with the function, but I was wondering if the same would be possible with zip, and if it would be simpler. – Quade Howald Aug 15 '19 at 14:23

1 Answers1

2

Yes! It is called reshape and can be done by Numpy. Check this out:

pd.DataFrame(df.values.reshape(-1, 3), columns=df.columns[:3]) 
ivallesp
  • 2,018
  • 1
  • 14
  • 21