具有日期时间索引的数据帧的列值的总和

时间:2017-03-29 08:11:52

标签: python pandas datetime dataframe

我有两个这样的数据框:

DF1:

public abstract class Transaction
{
    public int ID { get; set; }
    public string TransactionDescr { get; set; }
    public DateTime TransactionDate { get; set; }        
    public abstract int TransactionType { get; }
}

public class TransactAdd : Transact
{
    public override int TransactType
    {
        get
        {
            return 1;
        }
    }
}
public class TransactDeduct : Transact
{
    public override int TransactType
    {
        get
        {
            return 2;
        }
    }
}
public class TransactTransfer : Transact
{
    public override int TransactType
    {
        get
        {
            return 3;
        }
    }
}

DF2:

Timestamp    Consumption
2017-03-21    2903
2017-03-22    2982
2017-03-23    3011
2017-03-24    2964
2017-03-25    2961
2017-03-26    2967
2017-03-27    2967
2017-03-28    2903
2017-03-29    2923
2017-03-30    3032

我想将消费列的值添加到各自的日期,但我无法做到这一点。

期望的输出:

Timestamp             Consumption
2017-03-21 23:00:00    2000.0
2017-03-22 23:00:00    2005.0
2017-03-23 23:00:00    2026.0
2017-03-24 23:00:00    1919.0
2017-03-25 23:00:00    1933.0
2017-03-26 23:00:00    2006.0
2017-03-27 23:00:00    1995.0
2017-03-28 23:00:00    1998.0
2017-03-29 23:00:00    2002.0
2017-03-30 23:00:00    1730.0

我试过了:

Timestamp    Consumption
2017-03-21    4903
2017-03-22    4987
2017-03-23    5037
2017-03-24    4883
2017-03-25    4894
2017-03-26    4973
2017-03-27    4962
2017-03-28    4901
2017-03-29    4925
2017-03-30    4762

输出:

sum = df1['Consumption'] + df2['Consumption'][match(df1.index,df2.index)]

帮助我实现这一目标。

1 个答案:

答案 0 :(得分:0)

您可以为对齐数据创建索引,Timestamp中的列df2转换为date

df1 = df1.set_index('Timestamp')
s = df2.set_index(df2.Timestamp.dt.date)
df1['Consumption'] = df1['Consumption'].add(s['Consumption'])
#if necessary convert to int
df1['Consumption'] = df1['Consumption'].astype(int)
df1 = df1.reset_index()
print (df1)
   Timestamp  Consumption
0 2017-03-21         4903
1 2017-03-22         4987
2 2017-03-23         5037
3 2017-03-24         4883
4 2017-03-25         4894
5 2017-03-26         4973
6 2017-03-27         4962
7 2017-03-28         4901
8 2017-03-29         4925
9 2017-03-30         4762