I have the following in a file below and was wondering how to join them together, I know how to join if they were two separate dataframe but the values range from D1 up to D20+ and will not be practical to create so many multiple dataframes. My objective is to join them based on D1 date as per below.
D1 D1value D2 D2value D3 D3value
1/2/2018 21.14 1/2/2018 11.14 1/6/2018 1.55
1/3/2018 19.13 1/3/2018 51.14 1/13/2018 2.66
1/6/2018 19.89 1/14/2018 31.14 1/14/2018 3.77
1/13/2018 20.24
1/14/2018 20.91
After joining it should be
D1 D1value D2value D3value
1/2/2018 21.14 11.14 NaN
1/3/2018 19.13 51.14 NaN
1/6/2018 19.89 NaN 1.55
1/13/2018 20.24 NaN 2.66
1/14/2018 20.91 31.14 3.77
Any kind advise please?
For piRSquared answer add df:
df = pd.concat([proc(d) for k, d in df.groupby(g, 1)], axis=1)
Both piRSquared and jp_data_analysis answer works The issue is I want to select both as the right answer!
I'm assuming that columns come in adjacent pairs.
def proc(d):
v = d.dropna().values
return pd.Series(v[:, 1], pd.to_datetime(v[:, 0]), name=d.columns[1])
g = np.arange(len(df.columns)) // 2
pd.concat([proc(d) for k, d in df.groupby(g, 1)], axis=1)
D1value D2value D3value
2018-01-02 21.14 11.14 NaN
2018-01-03 19.13 51.14 NaN
2018-01-06 19.89 NaN 1.55
2018-01-13 20.24 NaN 2.66
2018-01-14 20.91 31.14 3.77
Setup
I'm assuming your file looks exactly like this:
D1,D1value,D2,D2value,D3,D3value
1/2/2018,21.14,1/2/2018,11.14,1/6/2018,1.55
1/3/2018,19.13,1/3/2018,51.14,1/13/2018,2.66
1/6/2018,19.89,1/14/2018,31.14,1/14/2018,3.77
1/13/2018,20.24,,,,
1/14/2018,20.91,,,,
I read it in with
df = pd.read_csv('test.csv')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With