2016-02-26 55 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