Home > front end >  creating a dataframe by using a list - python
creating a dataframe by using a list - python

Time:01-23

i have a list of 270 listitems and i would like to transfer to a dataframe. I would like to divide the content of the list into three dataframe columns containing 90 items per column. #

Col A Col B Col C

[0:89] [90:179] [180:269]

of course i could use List slicing and so on for this case. The problem is, that the number of items in the list is not fixed. It can vary. There can be also list length of 360,450,540 and so on (always 90 more items).

Is there anybody who can give me a hint to solve this problem? Thanks in advance Best regards Sascha

CodePudding user response:

You could simply use range and slices:

df = pd.DataFrame({f'Col{i   1}':
                  lst[i * 90: (i 1) * 90] for i in range(len(lst)//90)})

The good news is that this will silently ignore additional values if the length of the list is not an exact multiple of 90.

CodePudding user response:

use this piece of code

import pandas as pd

col_1 = your_list[0:89] 
col_2 =  your_list[90:179]
col_3 =  your_list[180:269]  
my_dict = {
    'Col A': col_1,
    'Col B': col_2,
    'Col C': col_3
}

df = pd.DataFrame(my_dict)
  •  Tags:  
  • Related