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

Time:10-21

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?

CodePudding user response:

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
  • Related