【问题标题】:Pandas: Difference between two series with different length (unaligned index)熊猫:不同长度的两个系列之间的差异(未对齐索引)
【发布时间】:2021-12-28 08:39:44
【问题描述】:

考虑以下两个系列:

sri = inp.groupby(inp.index.date)['value'].count()

2009-01-12    7
2009-01-14    3

sro = out.groupby(out.index.date)['value'].count()

2009-01-03      1
2009-01-09     14
2009-01-10     61
2009-01-11     93
2009-01-12    106
2009-01-13    123
2009-01-14    130

当我们从另一个中减去 sro-sri 时,我们有:

2009-01-03      NaN
2009-01-09      NaN
2009-01-10      NaN
2009-01-11      NaN
2009-01-12     99.0
2009-01-13      NaN
2009-01-14    127.0

但是我想要的输出是:

2009-01-03      1.0
2009-01-04      0.0
2009-01-05      0.0
2009-01-06      0.0
2009-01-07      0.0
2009-01-08      0.0
2009-01-09     14.0
2009-01-10     61.0
2009-01-11     93.0
2009-01-12     99.0
2009-01-13    123.0
2009-01-14    127.0

我们可以使用以下解决方法来生成相同的结果:

start_date = '2009-01-03'
end_date = '2009-01-15'
df = pd.DataFrame(
    index=pd.date_range(pd.to_datetime(start_date), pd.to_datetime(end_date) - timedelta(days=1), freq='d').date)
df = df.merge(sro.to_frame(), how='outer', left_index=True, right_index=True) \
    .merge(sri.to_frame(), how='outer', left_index=True, right_index=True).fillna(0)
print(df['value_x'] - df['value_y'])

是否有更紧凑的解决方案来生成相同的输出?

【问题讨论】:

    标签: python pandas dataframe datetime merge


    【解决方案1】:

    简单减法的一种简单方法是使用subfillna=0

    sro.sub(sri, fill_value=0).convert_dtypes()
    

    输出:

    2009-01-03      1
    2009-01-09     14
    2009-01-10     61
    2009-01-11     93
    2009-01-12     99
    2009-01-13    123
    2009-01-14    127
    

    添加缺失的索引:

    idx = sro.index.union(sri.index)
    (sro.sub(sri, fill_value=0)
        .reindex(pd.date_range(idx.min(), idx.max()).astype(str), fill_value=0)
        .convert_dtypes()
    )
    

    输出:

    2009-01-03      1
    2009-01-04      0
    2009-01-05      0
    2009-01-06      0
    2009-01-07      0
    2009-01-08      0
    2009-01-09     14
    2009-01-10     61
    2009-01-11     93
    2009-01-12     99
    2009-01-13    123
    2009-01-14    127
    

    使用的输入:

    sri = pd.Series({'2009-01-12': 7, '2009-01-14': 3})
    sro = pd.Series({'2009-01-03': 1, '2009-01-09': 14, '2009-01-10': 61, '2009-01-11': 93, '2009-01-12': 106, '2009-01-13': 123, '2009-01-14': 130})
    

    【讨论】:

    • 非常感谢您提供的非常简单的解决方案。云,请检查为缺少索引提供的代码吗?它为我产生零值!
    • @sci9 工作正常,我添加了 Series 构造函数以实现可重复性
    猜你喜欢
    • 1970-01-01
    • 2018-09-17
    • 1970-01-01
    • 1970-01-01
    • 2020-04-21
    • 1970-01-01
    • 2021-07-07
    • 2018-04-18
    • 2017-06-16
    相关资源
    最近更新 更多