Mainland
Mainland

Reputation: 4554

Python converting the lists of lists to a dataframe with common columns and index

My program generates data in each loop of a for loop function. I append to a list and finally I try to convert to the data into a single dataframe with common index and columns aligning automatically. However, I could not achieve this. My code:

biglist = []
# output of for loop in first iteration 
x1 = pd.DataFrame({'A':[11,22],'B':[33,44]},index=['x1','y1'])
biglist.append(x1)
# output of for loop in second iteration 
x2 = pd.DataFrame({'A':[110,220],'B':[330,440]},index=['x2','y2'])
biglist.append(x2)
# Now loop is over. Convert the biglist into a dataframe
df = pd.concat(biglist,axis=1)
print(df)

Present output:
      A     B      A      B
x1  11.0  33.0    NaN    NaN
y1  22.0  44.0    NaN    NaN
x2   NaN   NaN  110.0  330.0
y2   NaN   NaN  220.0  440.0
 
Expected output:
      A     B
x1  11.0   110.0
y1  22.0   220.0
x2  33.0   330.0
y2  44.0   440.0

UPDATE: My real problem is bigger. When I do pd.concat(biglist,axis=1), the output file size is [8 rows x 192 columns]. The 8 rows are perfect but 192 columns are repeated. But, when I do pd.concat(biglist,axis=0), the output file size is [64 rows x 24columns]. The 24 columns are perfect but 64 rows are repeated. Finally, the size of dataframe I am looking for is 8 rows x 24 columns. Here is the columns list.

Upvotes: 0

Views: 220

Answers (2)

Omni
Omni

Reputation: 1022

You have to concatenate it along axis 1 by

 df = pd.concat(biglist, axis=1)
 print(df)

    A    B
 x  11  110
 y  22  220

Upvotes: 1

Marat
Marat

Reputation: 15738

By default, pd.concat will stack dataframes vertically. Pass axis=1 to do it horizontally:

df = pd.concat(biglist, axis=1)

Upvotes: 2

Related Questions