Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

multiple series appended in a list using df.iterrows. How do I convert it into a dataframe?

I would like to know how to covert multiple series objects in list into a dataframe.

import pandas as pd

data = {
  "firstname": ["Sally", "Mary", "John"],
  "age": [50, 40, 30],
  "lastname" : ["a1", "b1", "c1"],
}

df = pd.DataFrame(data)

tmp_list = list()

for idx, row in df.iterrows():
    tmp_list.append(row)

My tmp_list looks like this.

[firstname    Sally
 age             50
 lastname        a1
 Name: 0, dtype: object,
 firstname    Mary
 age            40
 lastname       b1
 Name: 1, dtype: object,
 firstname    John
 age            30
 lastname       c1
 Name: 2, dtype: object]

How could I convert the multiple series objects in tmp_list into a dataframe like df?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

df

  firstname  age lastname
0     Sally   50       a1
1      Mary   40       b1
2      John   30       c1

>Solution :

Use concat then use transpose:

out=pd.concat(tmp_list,axis=1).T
'''
    firstname   age lastname
0   Sally       50  a1
1   Mary        40  b1
2   John        30  c1

'''

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading