2016-02-10 141 views
1

我有兩個Dataframes。熊貓/ Python - 均衡不同的索引

DF1:

     0   1   2 
Date                   
2005-10-01 772.142457 5600.491978 -5522.102692 
2005-11-01 445.861074 4866.226303 -4455.554864 
2005-12-01 -390.237513 923.679907 379.381452 
2006-01-01 -755.725402 673.734737 198.968080 
2006-02-01 -755.725402 673.734737 198.968080 

DF2:

   Value 
Date 
2005-07-01 -0.07920 
2005-08-01 -0.01412 
2005-09-01 -0.03646 
2005-10-01 0.17432 
2005-11-01 -0.05409 
2005-12-01 0.04988 
2006-01-01 -0.00232 

我希望他們有相同的指標,使DF1保持這樣的:

     0   1   2 
Date                   
2005-07-01   NaN   NaN   NaN 
2005-08-01   NaN   NaN   NaN 
2005-09-01   NaN   NaN   NaN 
2005-10-01 772.142457 5600.491978 -5522.102692 
2005-11-01 445.861074 4866.226303 -4455.554864 
2005-12-01 -390.237513 923.679907 379.381452 
2006-01-01 -755.725402 673.734737 198.968080 
2006-02-01 -755.725402 673.734737 198.968080 

而且DF2保持這樣的:

   Value 
Date 
2005-07-01 -0.07920 
2005-08-01 -0.01412 
2005-09-01 -0.03646 
2005-10-01 0.17432 
2005-11-01 -0.05409 
2005-12-01 0.04988 
2006-01-01 -0.00232 
2006-02-01  NaN 

我一直在尋找了一段時間來完成這件事,但我無法找到任何功能:(

任何人都可以幫助嗎?

回答

1

你想reindex並通過其他DF的union ED指數作爲新的指標,以重新編制反對:

In [135]: 
df.reindex(df1.index.union(df.index)) 

Out[135]: 
        0   1   2 
Date            
2005-07-01   NaN   NaN   NaN 
2005-08-01   NaN   NaN   NaN 
2005-09-01   NaN   NaN   NaN 
2005-10-01 772.142457 5600.491978 -5522.102692 
2005-11-01 445.861074 4866.226303 -4455.554864 
2005-12-01 -390.237513 923.679907 379.381452 
2006-01-01 -755.725402 673.734737 198.968080 
2006-02-01 -755.725402 673.734737 198.968080 

In [134]: 
df1.reindex(df.index.union(df1.index)) 
Out[134]: 
       Value 
Date    
2005-07-01 -0.07920 
2005-08-01 -0.01412 
2005-09-01 -0.03646 
2005-10-01 0.17432 
2005-11-01 -0.05409 
2005-12-01 0.04988 
2006-01-01 -0.00232 
2006-02-01  NaN 

您可以先創建組合索引,並把它傳遞給上面,使其更具可讀性:

In [136]: 
combined_idx = df.index.union(df1.index) 
combined_idx 

Out[136]: 
DatetimeIndex(['2005-07-01', '2005-08-01', '2005-09-01', '2005-10-01', 
       '2005-11-01', '2005-12-01', '2006-01-01', '2006-02-01'], 
       dtype='datetime64[ns]', name='Date', freq=None) 

所以df.reindex(combined_idx)df1.reindex(combined_idx)將工作