How Can I Add my Nested List to Pandas DataFrame as a row ? (Best way)

Question:

I have an nested list like that;

[[1,2,3,...,48], [96,97,98,...,144], [145,...,192]] –> means always list of length in inside is 48

and I have empty pandas dataframe like that (I wrote nan values ​​to show better.);

   one two three ... forty-eight
0  NaN NaN  NaN  ...     NaN

I want add nested list element (which is every single list) to dataframe as a row.

   one two three  ... forty-eight
0  1    2   3     ...     48
1  49   50  51    ...     99

How do I do this in the best way?

Asked By: Tugrul Gokce

||

Answers:

The best way is to create directly DataFrame:

import pandas as pd

data = [ 
    [1,2,3], 
    [4,5,6], 
    [7,8,9] 
]

df = pd.DataFrame(data, columns=['one', 'two', 'three'])

print(df)

Result:

   one  two  three
0    1    2      3
1    4    5      6
2    7    8      9
Answered By: furas
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.