Python 2.7 如何将一个数据帧中的所有小时值与另一个数据帧中的年值相乘?
我有以下每小时数据帧dfA:Python 2.7 如何将一个数据帧中的所有小时值与另一个数据帧中的年值相乘?,python-2.7,pandas,dataframe,criteria,multiplication,Python 2.7,Pandas,Dataframe,Criteria,Multiplication,我有以下每小时数据帧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 我还有第二个数据帧dfB,带有年度变量
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
我还有第二个数据帧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
我想用数据帧dfB中相应年份的系数乘以dfA中的每小时值
结果应该如下所示:
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”对象不可调用
。
有人能帮我吗?说你从
dfA = pd.DataFrame({
'Date/Time': ['01.03.2010 00:00:00'],
'Value1': [60],
'Value2': [10]})
dfB = pd.DataFrame({
'Date/Time': ['01.03.2010'],
'Value1': [1.5],
'Value2': [0.9]})
在每个数据框中添加一个'year'
列:
dfA['year'] = pd.to_datetime(dfA['Date/Time'])
dfB['year'] = pd.to_datetime(dfB['Date/Time'])
现在,在该列左侧合并:
>>> pd.merge(
dfA,
dfB,
left_on='year',
right_on='year',
how='left')
Date/Time_x Value1_x Value2_x year Date/Time_y Value1_y Value2_y
0 01.03.2010 00:00:00 60 10 2010-01-03 01.03.2010 1.5 0.9
请注意,合并添加了后缀“ux”
和“uy”
在合并之后,您可以按常规方式对列进行乘法,去掉不需要的列,等等。最简单的方法可能是将较低频率的序列/数据帧采样到较高频率,然后进行乘法
In [82]: s1 = pd.Series(np.random.randn(31), index=pd.date_range(start='2015-01-01', end='2015-01-31', freq='d'))
In [83]: s2 = pd.Series(np.random.randn(4), index=pd.date_range(start='2015-01-01', end='2015-01-31', freq='W'))
In [84]: s2.resample('d').mean().reindex(s1.index).ffill().bfill() * s1
Out[84]:
2015-01-01 0.361944
2015-01-02 2.806391
2015-01-03 0.741745
2015-01-04 0.855619
2015-01-05 -2.127828
...
2015-01-27 0.533919
2015-01-28 0.792278
2015-01-29 1.722754
2015-01-30 0.822032
2015-01-31 0.729741
Freq: D, dtype: float64
在这种情况下,我们需要额外的
redindex(s1.index)
,因为起点和终点不完全对齐。您可以尝试将df1.index.year
的索引追加到df1.index.year
的索引中,然后将df2
的索引更改为years
,然后使用:
美好的我不知道你可以直接乘以数据帧。谢谢你的回答。这种方式在我的代码中似乎效果最好。谢谢你的回答。谢谢你的回答。
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