1

I have list vector_list of length 800,000, where the elements are lists of size 768. I'm trying to add 768 columns to a pandas dataframe where each column is 800,000 long and represents an element from each list. Here's my code:

active = pd.DataFrame()    
for i in range(len(vector_list[0])):
    element_list = []
    for j in range(len(vector_list)):
        element_list.append(vector_list[j][i])
    active['Element {}'.format(i)] = element_list

Just to reiterate,

len(vector_list) = 800,000
len(vector_list[0]) = 768

Is there a more clever, faster way to do this?

3
  • 2
    pd.DataFrame(vector_list) ? Commented Dec 8, 2019 at 16:17
  • Pass your vector list to the pd.DataFrame constructor and use the names parameter and insert it the names of the columns ? Commented Dec 8, 2019 at 16:18
  • Yep that's all I needed, thank you. Commented Dec 8, 2019 at 16:25

1 Answer 1

3

Directly pass the list to DataFrame constructor.

import pandas as pd
_list = [[1, 2], [3, 4], [5, 6], [7, 8]] 
df = pd.DataFrame(_list) 
print(df.head())

Output

   0  1
0  1  2
1  3  4
2  5  6
3  7  8
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.