2016-02-26 59 views
1

有2个dataframes,是否可以使用DataFrame.mul()fill_value进行填充?

df 

other 

,用相同的专栏中,我可以看到fill_value参数:

DataFrame.mul(other, fill_value=...) 

有如下解释:

fill_value : None or float value, default None 
Fill missing (NaN) values with this value. If both DataFrame locations are missing, the result will be missing 

我该怎么做乘法与填充前的行为,使得对于df不在other那些行,我将填补前行中的其他,仍然得到结果行?

下面是一个例子:

df

1/1/2016 10 
2/1/2016 20 
3/1/2016 25 

other

1/1/2016 1.5 
3/1/2016 1.7 

我想df.mul(其他),就好像其他必须采取行动

2/1/2016 1.5 

以及

+0

你能显示什么示例输入和输出数据框看起来像? –

+0

已更新原始文章。 – MMM

回答

1

您可以重新编制other第一:

df.mul(other.reindex(df.index, method='ffill')) 

例子:

>>> df 
      1 
2016-01-01 10 
2016-02-01 20 
2016-03-01 25 

>>> other 
       1 
2016-01-01 1.5 
2016-03-01 1.7 

>>> df.mul(other.reindex(df.index, method='ffill')) 
       1 
2016-01-01 15.0 
2016-02-01 30.0 
2016-03-01 42.5 
0

你可以先reindexreindex_likefillna然后用mul

print df 
      A 
2016-01-01 10 
2016-02-01 20 
2016-03-01 25 

print df1 
       A 
2016-01-01 1.5 
2016-03-01 1.7 

df1 = df1.reindex(df.index).fillna(method='ffill') 
#df1 = df1.reindex_like(df).fillna(method='ffill') 
print df1 

       A 
2016-01-01 1.5 
2016-02-01 1.5 
2016-03-01 1.7 


print df.mul(df1) 
       A 
2016-01-01 15.0 
2016-02-01 30.0 
2016-03-01 42.5