Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 基于来自另一个数据帧的值更新数据帧_Python_Pandas_Dataframe - Fatal编程技术网

Python 基于来自另一个数据帧的值更新数据帧

Python 基于来自另一个数据帧的值更新数据帧,python,pandas,dataframe,Python,Pandas,Dataframe,我有两个数据帧,如下所示: A B date 2017-10-5 2 3 2017-10-6 5 5 2017-11-5 7 8 2017-11-6 11 13 W1 W2 date 2017-09-30 -0.2 0.01 2017-10-31 -0.003 0.04 我想创建一个包含以下内容的新数据框: W1 * A

我有两个数据帧,如下所示:

            A   B
date        
2017-10-5   2   3
2017-10-6   5   5
2017-11-5   7   8
2017-11-6   11  13


             W1     W2
date        
2017-09-30  -0.2    0.01
2017-10-31  -0.003  0.04
我想创建一个包含以下内容的新数据框:

            W1 * A       W2 * B
date        
2017-10-5   -0.2 * 2     0.01 * 3
2017-10-6   -0.2 * 5     0.01 * 5
2017-11-5   -0.003 * 7   0.04 * 8
2017-11-6   -0.003 * 11  0.04 * 13

使用
np。在
df2
上重复
,然后相乘。看起来索引在这里不起作用

df1 = df1.mul(np.repeat(df2.values, 2, axis=0))
或者,更一般地说

df1 = df1.mul(np.repeat(df2.values, len(df1) // len(df2), axis=0))
print(df1)
               A     B
date                  
2017-10-5 -0.400  0.03
2017-10-6 -1.000  0.05
2017-11-5 -0.021  0.32
2017-11-6 -0.033  0.52

其中
len(df1)//len(df2)
计算其大小的比率。

如果索引确实有意义,即您有一个在某个日期更改的值,并且希望继续使用它,直到下次更改为止。然后可以使用参数
method='ffill'
使用
reindex
命令创建与原始数据帧对齐的数据帧。下面是它的样子:

import pandas as pd
import dateutil

df = pd.DataFrame([['2017-10-5',2,3],
                  ['2017-10-6',5,5],
                  ['2017-11-5',7,8],
                  ['2017-11-6',11,13]],
                  columns = ['date','A','B'])
df['date'] = df['date'].apply(dateutil.parser.parse)
df = df.set_index('date')

wdf = pd.DataFrame([['2017-09-30',-0.2,0.01],
                    ['2017-10-31',-0.03,0.04]],
                     columns=['date','W1','W2'])
wdf['date'] = wdf['date'].apply(dateutil.parser.parse)
wdf = wdf.set_index('date')
wdf_r = wdf.reindex(df.index,
                    method='ffill')

res = df.drop(['A','B'],axis=1).assign(W1_x_A = wdf_r.W1 * df.A,
                                       W2_x_B = wdf_r.W2 * df.B)
print(res)
哪个输出

            W1_x_A  W2_x_B
date                      
2017-10-05   -0.40    0.03
2017-10-06   -1.00    0.05
2017-11-05   -0.21    0.32
2017-11-06   -0.33    0.52

索引的日期是多少?为什么要将不匹配的索引相乘?索引在这里确实不起作用。谢谢你的及时回复!