How do you build a pandas DataFrame from a list of pandas.core.frame.Pandas elements?

Code example illustrating question:

I have the DataFrame below:

import pandas as pd     
df = pd.DataFrame({'col1': [1, 2], 'col2': [0.1, 0.2]}, index=['a', 'b'])


   col1  col2
a     1   0.1
b     2   0.2

I then iterate through it to make a list of the rows:

data_list = []
for row in df.itertuples():
    data_list.append(row)

[Pandas(Index='a', col1=1, col2=0.1), Pandas(Index='b', col1=2, col2=0.2)]

When I check the datatype I get:

for i in data_list:
    print(type(i))

<class 'pandas.core.frame.Pandas'>
<class 'pandas.core.frame.Pandas'>

How do I re-construct this data_list into a DataFrame?

I've tried Counter from Collections to convert into a dictionary, but I can't get this to work. Not clear if this is even the best way to solve the problem

1 Answers

1
yatu On Best Solutions

the pd.DataFrame constructor goes perfectly well with named tuples and also constructs the columns from the names:

pd.DataFrame(data_list).set_index('Index')

        col1  col2
Index            
a         1   0.1
b         2   0.2