Merge (numpy) Arrays Based On Date
I have N arrays each structured as the following Array 1: [['2014-01-01', '2014-01-03' ...], [1.1, 0.5, ...]] Array 2: [['2014-01-01', '2014-01-02' ...], [1.4, 0.9, ...]] Array 3:
Solution 1:
This should solve it, using merge
function and outer
method
>>> import pandas as pd
>>> import numpy as np
>>> d1 = pd.DataFrame(np.array([['2014-01-01', '2014-01-03'], [1.1, 0.5]])).T
>>> d2 = pd.DataFrame(np.array([['2014-01-01', '2014-01-02'], [1.4, 0.9]])).T
>>> d3 = pd.DataFrame(np.array([['2014-01-02', '2014-01-04'], [0.8, 1.5]])).T
>>> d1.columns = d2.columns = d3.columns = ['t','v']
>>> pd.DataFrame(np.array(d1.merge(d2, on='t', how='outer').
... merge(d3, on='t', how='outer').
... sort('t')),
... columns=['date','1-data','2-data','3-data'])
...
date 1-data 2-data 3-data
0 2014-01-01 1.1 1.4 NaN
1 2014-01-02 NaN 0.9 0.8
2 2014-01-03 0.5 NaN NaN
3 2014-01-04 NaN NaN 1.5
[4 rows x 4 columns]
Post a Comment for "Merge (numpy) Arrays Based On Date"