sfortney sfortney - 2 months ago 17
Python Question

Python Pandas - Combining Multiple Columns into one Staggered Column

How do you combine multiple columns into one staggered column? For example, if I have data:

Column 1 Column 2
0 A E
1 B F
2 C G
3 D H

And I want it in the form:

Column 1
0 A
1 E
2 B
3 F
4 C
5 G
6 D
7 H

What is a good, vectorized pythonic way to go about doing this? I could probably do some sort of df.apply() hack but I'm betting there is a better way. The application is putting multiple dimensions of time series data into a single stream for ML applications.


First stack the columns and then drop the multiindex:

0    A
1    E
2    B
3    F
4    C
5    G
6    D
7    H
dtype: object