简体   繁体   中英

How to convert for-loop generated dictionaries list to dataframe in python

for-loop generating multiple lists and each list having, multiple dictionaries with similar 'key' names and want to make similar 'key' names as column names and values should be assigned to the value column in the dataframe.

Eg:

The outcome of 1st for loop

[{'name':'kumar','job':'Plumber'},{'name':'Rajesh','job':'Painter']

The outcome of 2nd for loop

[{'name':'Jaslin','job':'Teacher'},{'name':'Ramu','job':'Engineer']


df:

    name    job
0   Kumar   Plumber
1   Rajesh  Painter
2   Jaslin  Teacher
3   Ramu    Engineer

I'm beginner how to convert for-loop generated list to dataframe.

Just combine both lists and send it as a parameter to DataFrame

l1 = [{'name': 'kumar', 'job': 'Plumber'}, {'name': 'Rajesh', 'job': 'Painter'}]
l2 = [{'name': 'Jaslin', 'job': 'Teacher'}, {'name': 'Ramu', 'job': 'Engineer'}]
df = pd.DataFrame(l1 + l2)

Output

     name       job
0   kumar   Plumber
1  Rajesh   Painter
2  Jaslin   Teacher
3    Ramu  Engineer

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM