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

Make a dataframe from list of variables which are lists

I have a list of variables in which each variable is a list. And I want to use that list to form a dataframe.

A=[1,2]
B=[4,3]
C=[A,B]

I want to create the dataframe using the list C that looks like this:

A B
1 4
2 3

I tried doing it like this

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

headers=['A','B']
df = pd.DataFrame(C, columns=headers)

But it doesn’t work. Any suggestions on how to achieve it?

>Solution :

headers=['A','B']
df = pd.DataFrame(columns=headers)

for i in range(len(headers)):
    df[headers[i]] = pd.Series(C[i])
#output
   A  B
0  1  4
1  2  3
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