【问题标题】:Python DataFrames: Appending dataframes or series and overwriting by indexPython DataFrames:附加数据帧或系列并按索引覆盖
【发布时间】:2020-08-13 00:13:57
【问题描述】:

我将有一个迭代过程,按索引创建一组相似但不相同的熊猫系列。

我想将一个附加到另一个,但在适用的情况下覆盖。例如

系列一:

Index Value
Jan    3
Feb    5
Mar    4

系列二:

Index Value
Mar    10
Apr    5
May    7

期望的输出:

Jan    3
Feb    5
Mar    10
Apr    5
May    7

提前致谢。

【问题讨论】:

标签: python pandas indexing append


【解决方案1】:

让我们做吧

s=s2.combine_first(s1)
Index
Apr     5.0
Feb     5.0
Jan     3.0
Mar    10.0
May     7.0
Name: Value, dtype: float64

或者

s1.append(s2).groupby(level=0).tail(1)
Index
Jan     3
Feb     5
Mar    10
Apr     5
May     7
Name: Value, dtype: int64

【讨论】:

    猜你喜欢
    • 2020-01-24
    • 2022-01-26
    • 2015-08-03
    • 1970-01-01
    • 1970-01-01
    • 2018-08-23
    • 2013-04-09
    • 2021-10-10
    • 2020-09-18
    相关资源
    最近更新 更多