Reputation: 835
A database in US receives one day information from a place around the world in .csv
format. There are 15 days of data and coming from 26 places. A total of 15x26 = 390 data frames. In addition, 26 places have a common reference data frame. Now, I want to combine 390 + 1 reference dataframes into one. I have given here a sample of my problem:
plA_d1df = ### place A day1 dataframe
Adata
2019-01-10 07:35:22 10
2019-01-10 08:15:17 20
plB_d1df =
Bdata
2019-01-10 07:38:45 30
2019-01-10 08:18:57 40
ptA_d2df =
Adata
2019-01-21 07:35:42 50
2019-01-21 08:15:17 60
ptB_d2df =
Bdata
2019-01-21 07:39:04 70
2019-01-21 08:19:22 80
reference =
ref
2019-01-10 07:35:00 500
2019-01-10 07:38:00 530
2019-01-10 08:15:00 560
2019-01-10 08:18:00 590
2019-01-21 07:35:00 610
2019-01-21 07:39:00 640
2019-01-21 08:15:00 670
2019-01-21 08:19:00 700
Above data of all places and reference should be combined to the timestamp of place-A as given below:
combdf =
datetime ref0 Adata ref1 Bdata
2019-01-10 07:35:22 500 10 530 30
2019-01-10 08:15:17 560 20 590 40
2019-01-21 07:35:42 610 50 640 70
2019-01-21 08:15:17 670 60 700 80
I implemented following code after referring the solved answer:
biglist = [[plA_d1df,plB_d1df],[plA_d2df,plB_d2df]] ## dataframes are in a nested list of list
l = []
s1 = []
### refdf = reference dataframe
for i in range(0,len(biglist),1):
for j in range(0,len(biglist[i]),1):
s1=refdf.reindex(biglist[i][j].index,method='nearest')
if j==0:
l.append(s1.join(biglist[i][j]))
else:
l.append(s1.join(biglist3[i][j]).reindex(l[0].index,method='nearest'))
combdf = pd.concat(l,1)
Above code ran successfully. Timestamp of combined dataframe combdf
matches with place A, which is what I wanted. But the columns of same place did not merge. Instead, seperate columns were created for each day. So I eneded up having 8 columns, instead 4, mostly filled with nan
.
My present output is:
combdf =
datetime ref0 Adata ref1 Bdata ref0 Adata ref1 Bdata
2019-01-10 07:35:22 500 10 530 30 nan .. nan
2019-01-10 08:15:17 560 20 590 40 nan .. nan
2019-01-21 07:35:42 nan .. nan 610 50 640 70
2019-01-21 08:15:17 nan .. nan 670 60 700 80
What corrections I have to make to merge columns into same.
Upvotes: 2
Views: 127
Reputation: 323326
Change your code to
biglist = [[df1,df2],[df3,df4]] ## dataframes are in a nested list of list
l = []
s1 = []
for i in range(0,len(biglist),1):
l1=[]
for j in range(0,len(biglist[i]),1):
s1=refdf.reindex(biglist[i][j].index,method='nearest')
if j==0:
l1.append(s1.join(biglist[i][j]))
else:
l1.append(s1.join(biglist[i][j]).reindex(l1[0].index,method='nearest'))
l.append(pd.concat(l1,axis=1))
combdf = pd.concat(l,0)
combdf
Out[252]:
ref Adata ref Bdata
2019-01-10 07:35:22 500 10 530 30
2019-01-10 08:15:17 560 20 590 40
2019-01-21 07:35:42 610 50 640 70
2019-01-21 08:15:17 670 60 700 80
Upvotes: 2