具有日期时间指数和不同长度的三个系列的总和?

时间:2017-07-21 09:32:46

标签: python pandas series

我有三个系列:

系列1:

Timestamp
2017-04-02    21666.45
2017-04-03    21666.45
2017-04-04    21666.45
2017-04-05    21666.45
2017-04-06    21666.45
Freq: D, Name: Price, dtype: float64

系列2:

Timestamp
2017-04-02    9918.6
2017-04-03    9918.6
2017-04-04    9918.6
2017-04-05    9918.6
2017-04-06    9918.6
Freq: D, Name: Price, dtype: float64

系列3:

Timestamp
    2017-04-02    9769.55
    2017-04-03    9769.55
    2017-04-04    9769.55
    2017-04-05    9769.55
    2017-04-06    9769.55
    2017-04-07    9769.55
    Freq: D, Name: Price, dtype: float64

但我得到的总和是将所有系列添加到2017-04-06只作为:

normal_sum['Price'] + peak_sum['Price'] + off_sum['Price']

Timestamp
2017-04-02    41354.6
2017-04-03    41354.6
2017-04-04    41354.6
2017-04-05    41354.6
2017-04-06    41354.6
2017-04-07    Nan

预期产出:

Timestamp
2017-04-02    41354.6
2017-04-03    41354.6
2017-04-04    41354.6
2017-04-05    41354.6
2017-04-06    41354.6
2017-04-07    9769.55

任何帮助将不胜感激。感谢。

2 个答案:

答案 0 :(得分:0)

我认为您需要Series.add参数df = normal_sum['Price'].add(peak_sum['Price'], fill_value=0) .add(off_sum['Price'], fill_value=0) print (df) Timestamp 2017-04-02 41354.60 2017-04-03 41354.60 2017-04-04 41354.60 2017-04-05 41354.60 2017-04-06 41354.60 2017-04-07 9769.55 Name: Price, dtype: float64

from functools import reduce
cols = [normal_sum['Price'], peak_sum['Price'], off_sum['Price']]

df = reduce(lambda l, r: l.add(r, fill_value=0), cols)
print (df)
Timestamp
2017-04-02    41354.60
2017-04-03    41354.60
2017-04-04    41354.60
2017-04-05    41354.60
2017-04-06    41354.60
2017-04-07     9769.55
Name: Price, dtype: float64

有点改变Yakym Pirozhenko' s comment

<TX value="com.igsapp.wibc.host.TxInfo">
<FORMAT>
    <RECORD id="RqHeader">
        <ITERATION id="Header" times="1">
            <FIELD id="Id"/>
            <FIELD id="CAppSeq"/>
            <FIELD id="CAGrpSeq"/>
            <ITERATION id="Status" times="1">
                <FIELD id="System"/>
                <FIELD id="SCode"/>
            </ITERATION>
            <ITERATION id="aaa" times="1">
                <FIELD id="bbb"/>
                <FIELD id="Sccc"/>
            </ITERATION>
        </ITERATION>
   </RECORD>
 </FORMATE>
 </TX>

答案 1 :(得分:0)

在这种情况下,我通常使用pd.concat,它负责加入索引:

df = pd.concat([normal_sum['Price'], peak_sum['Price'], off_sum['Price']],
                axis=1).sum(axis=1)