简体   繁体   English

使用列表创建 dataframe - python

[英]creating a dataframe by using a list - python

i have a list of 270 listitems and i would like to transfer to a dataframe.我有 270 个列表项的列表,我想转移到 dataframe。 I would like to divide the content of the list into three dataframe columns containing 90 items per column.我想将列表的内容分成三个 dataframe 列,每列包含 90 个项目。 # #

Col A Col B Col C Col A Col B Col C

[0:89] [90:179] [180:269] [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).列表长度也可以是 360,450,540 等等(总是 90 多个项目)。

Is there anybody who can give me a hint to solve this problem?有没有人可以给我一个提示来解决这个问题? Thanks in advance Best regards Sascha在此先感谢 最好的问候萨沙

You could simply use range and slices:您可以简单地使用range和切片:

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.好消息是,如果列表的长度不是 90 的精确倍数,这将默默地忽略其他值。

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM