简体   繁体   中英

Python: How to convert list of lists into data frame?

I currently have a lists of lists which I have converted to a dataframe as you can see in the screenshot. I would like to break up each list into separate columns. Since each list has 5 entries, the resulting dataframe would be 10 columns by 720 rows. Does anyone know how to efficiently do this? I can create a bunch of for loops, but it is not efficient in my eyes. Thank you!!

在此处输入图像描述

Assuming that you are given two 720 by 5 lists a and b as in columns 0 and 1 respectively (here mocked by random values for demonstration), you could eg create two separate pd.DataFrame s first and then merge them using pd.concat :

import pandas as pd
import numpy as np

a = np.random.random((720, 5)).tolist()
b = np.random.random((720, 5)).tolist()

df = pd.concat([pd.DataFrame(a), pd.DataFrame(b)], axis=1)
print(df.shape)

prints

(720, 10)

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