加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python-2.7 – 如何将一个pandas数据帧中的所有小时值与另一个数

发布时间:2020-12-20 11:49:39 所属栏目:Python 来源:网络整理
导读:我有以下每小时数据帧dfA: Date/Time Value1 Value201.03.2010 00:00:00 60 1001.03.2010 01:00:00 50 20 01.03.2010 02:00:00 52 3001.03.2010 03:00:00 49 40...31.12.2013 23:00:00 77 50 我有一个年度vaules的第二个数据帧dfB: Date/Time Value1 Value
我有以下每小时数据帧dfA:

Date/Time            Value1    Value2
01.03.2010 00:00:00  60        10
01.03.2010 01:00:00  50        20 
01.03.2010 02:00:00  52        30
01.03.2010 03:00:00  49        40
.
.
.
31.12.2013 23:00:00  77        50

我有一个年度vaules的第二个数据帧dfB:

Date/Time   Value1    Value2
31.12.2010   1.5        0.9
31.12.2011   1.6        1.1 
31.12.2012   1.7        2.3
31.12.2013   1.3        0.6

我想将dfA中的每小时值乘以数据帧dfB中相应年份的因子.

结果应如下所示:

Date/Time            Value1    Value2
01.03.2010  00:00:00    90        9
01.03.2010  01:00:00    75       18
01.03.2010  02:00:00    78       27
01.03.2010  03:00:00    73.5     36
.           
.           
.           
31.12.2013  23:00:00    100.1    30

我一直在尝试使用dfC = dfA * dfB [dfA.index.year()],但我收到错误TypeError:’numpy.ndarray’对象不可调用.
谁能帮我这个?

解决方法

您可以尝试附加到df1 df1.index.year的索引,然后将df2的索引更改为years,然后使用 mul

print df1
                     Value1  Value2
Date/Time                          
2010-01-03 00:00:00      60      10
2010-01-03 01:00:00      50      20
2010-01-03 02:00:00      52      30
2010-01-03 03:00:00      49      40
2013-12-31 23:00:00      77      50

print df2
            Value1  Value2
Date/Time                 
2010-12-31     1.5     0.9
2011-12-31     1.6     1.1
2012-12-31     1.7     2.3
2013-12-31     1.3     0.6

df1 = df1.set_index(df1.index.year,append=True)
df2.index = df2.index.year

????

print df1
                          Value1  Value2
Date/Time                               
2010-01-03 00:00:00 2010      60      10
2010-01-03 01:00:00 2010      50      20
2010-01-03 02:00:00 2010      52      30
2010-01-03 03:00:00 2010      49      40
2013-12-31 23:00:00 2013      77      50

print df2
      Value1  Value2
2010     1.5     0.9
2011     1.6     1.1
2012     1.7     2.3
2013     1.3     0.6

print df1.mul(df2,level=1).reset_index(drop=True,level=1)
Value1  Value2
Date/Time                          
2010-01-03 00:00:00    90.0       9
2010-01-03 01:00:00    75.0      18
2010-01-03 02:00:00    78.0      27
2010-01-03 03:00:00    73.5      36
2013-12-31 23:00:00   100.1      30

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读