vendredi 10 juin 2016

What is the most efficient way to create a pandas dataframe that combine two unrelated series?


I'm looking at creating a Dataframe that is the combination of two unrelated series.

If we take two dataframes:

A = ['a','b','c']
B = [1,2,3,4]

dfA = pd.DataFrame(A)
dfB = pd.DataFrame(B)

I'm looking for this output:

    A  B
0   a  1
1   a  2
2   a  3
3   a  4
4   b  1
5   b  2
6   b  3
7   b  4
8   c  1
9   c  2
10  c  3
11  c  4

One way could be to have loops on the lists direclty and create the DataFrame but there must be a better way. I'm sure I'm missing something from the pandas documentation.

result = []
for i in A:
    for j in B:
        result.append([i,j])

result_DF = pd.DataFrame(result,columns=['A','B'])

Ultimately I'm looking at combining months and UUID, I have something working but it takes ages to compute and relies too much on the index. A generic solution would clearly be better:

from datetime import datetime

start = datetime(year=2016,month=1,day=1)
end = datetime(year=2016,month=4,day=1)
months = pd.DatetimeIndex(start=start,end=end,freq="MS")
benefit = pd.DataFrame(index=months)

A = [UUID('d48259a6-80b5-43ca-906c-8405ab40f9a8'),
   UUID('873a65d7-582c-470e-88b6-0d02df078c04'),
   UUID('624c32a6-9998-49f4-92b6-70e712355073'),
   UUID('7207ab0c-3c7f-477e-b5bc-fbb8059c1dec')]
dfA = pd.DataFrame(A)

result = pd.DataFrame(columns=['A','month'])
for i in dfA.index:
    newdf = pd.DataFrame(index=benefit.index)
    newdf['A'] = dfA.iloc[i,0]
    newdf['month'] = newdf.index
    result = pd.concat([result,newdf])
result

Aucun commentaire:

Enregistrer un commentaire