I have a large set of date-stamped files whose filenames are dates. Each file has a set of records, with the same structure, that is in a dataframe
.
I would like to make these into a single dataframe
, with the filename as a column in it.
The sample code is below.
import pandas as pd
import numpy as np
df1 = pd.DataFrame({
'a': np.random.rand(6),
'b': np.random.choice([4,9,np.nan], 6),
})
df2 = df1*3
df3 = np.sqrt(df2)
df4 = df1*df3
df5 = df2*0.5
df6 = df2*df2
df = pd.DataFrame({
'fname': pd.date_range('1/1/2018', periods=6, freq='D'),
'df': [df1, df2, df3, df4, df5, df6]
})
Output df expected is:
a b fname
0 0.634776 NaN 2018-01-01
1 0.616349 NaN 2018-01-02
2 0.502559 NaN 2018-01-03
3 0.545378 4.0 2018-01-04
4 0.658668 4.0 2018-01-05
5 0.822417 9.0 2018-01-06
0 1.904328 NaN 2018-01-01
1 1.849046 NaN 2018-01-02
2 1.507678 NaN 2018-01-03
3 1.636133 2.0 2018-01-04
4 1.976004 2.0 2018-01-05
5 2.467252 3.0 2018-01-06
...and so on...