Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to join two pandas Series into a single one with interleaved values?

I have two pandas.Series...

import pandas as pd
import numpy as np

length = 5
s1 = pd.Series( [1]*length ) # [1, 1, 1, 1, 1]
s2 = pd.Series( [2]*length ) # [2, 2, 2, 2, 2]

...and I would like to have them joined together in a single Series with the interleaved values from the first 2 series. Something like: [1, 2, 1, 2, 1, 2, 1, 2, 1, 2]

like image 698
luca Avatar asked Dec 19 '22 13:12

luca


1 Answers

Using np.column_stack:

In[27]:pd.Series(np.column_stack((s1,s2)).flatten())
Out[27]: 
0    1
1    2
2    1
3    2
4    1
5    2
6    1
7    2
8    1
9    2
dtype: int64
like image 167
shivsn Avatar answered Jan 13 '23 03:01

shivsn