python - 如何将两个 Pandas 系列加入一个具有交错值的系列?

标签 python pandas numpy

我有两个 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]

...我想让它们与前 2 个系列中的交错值一起加入一个系列中。 类似于:[1, 2, 1, 2, 1, 2, 1, 2, 1, 2]

最佳答案

使用 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

关于python - 如何将两个 Pandas 系列加入一个具有交错值的系列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39293328/

相关文章:

python - 通过一道超越时间限制的竞赛题来了解 Python 的速度

python - 在 Bokeh 中显示词云的图像

python - Pandas 根据其他行的和/差添加新行

python - 溢出错误: size does not fit in an int

python - C 中的 Numpy 数组

python - 替换多维数组的元素

python - 使用 python requests 和 BeatifulSoup 在维基百科页面上抓取多个表及其标题?

python - float64 与 Pandas to_csv

python - 使用条件总和的结果创建 Pandas DataFrame 列

python - 比较 NumPy 对象引用