python - 将两个系列合并为一个索引不匹配的系列
问题描述
我正在尝试将两个索引不匹配的系列合并为一个,我想知道最佳实践是什么。
我尝试了 combine_first 但我遇到了一个问题,将系列 [0, 24, ...] 与系列 [1, 25, ...] 组合应该给出一个带有索引 [0, 1, 24, 25, ...] 但我得到 [0, 12, 24, 36, ...]
for row in all_rows:
base_col = base_col.combine_first(row)
我只想将具有互斥索引的两个系列组合成一个系列,其中包含两个索引的正确排序顺序。有点像拉链。
解决方案
您可以使用pd.concat
后跟sort_index
:
s1 = pd.Series([1, 2, 3, 4], index=[0, 24, 30, 40])
s2 = pd.Series([5, 6, 7, 8], index=[1, 25, 35, 38])
s = pd.concat([s1, s2]).sort_index()
print(s)
0 1
1 5
24 2
25 6
30 3
35 7
38 8
40 4
dtype: int64
推荐阅读
- react-native - 当我使用 react-native-maps 时,它给了我这个错误
- c# - 如何在 C# 中模拟基类属性或方法
- computer-vision - 图像坐标中的 ARKit 关键点 (2D)
- jakarta-ee - 使用动态配置键的 Microprofile 配置
- javascript - 如何通过javascript设置新添加单元格的html属性
- javascript - jquery这个参数用于选择不工作的孩子
- python-3.x - 张量流中标量的批量乘法/除法
- node.js - 带有mssql和节点的docker stack,如何正确连接到数据库?
- python - 通过类型注释在 Python 中对类型构造函数进行抽象
- java - 如何使用 Swagger codegen 在 Java 中生成复制构造函数