将多个列表放入数据框中 [英] Take multiple lists into dataframe

查看:34
本文介绍了将多个列表放入数据框中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获取多个列表并将它们作为不同的列放在 python 数据框中?我尝试了这个解决方案,但遇到了一些麻烦.

How do I take multiple lists and put them as different columns in a python dataframe? I tried this solution but had some trouble.

尝试 1:

  • 有三个列表,并将它们压缩在一起并使用 res = zip(lst1,lst2,lst3)
  • 只产生一列

尝试 2:

percentile_list = pd.DataFrame({'lst1Tite' : [lst1],
                                'lst2Tite' : [lst2],
                                'lst3Tite' : [lst3] }, 
                                columns=['lst1Tite','lst1Tite', 'lst1Tite'])

  • 产生一行乘 3 列(上面的方式),或者如果我转置它是 3 行和 1 列
  • 如何通过 3 列(三个列表)pandas 数据框获得 100 行(每个独立列表的长度)?

    How do I get a 100 row (length of each independent list) by 3 column (three lists) pandas dataframe?

    推荐答案

    我想你已经差不多了,试着去掉 lst 周围的额外方括号(你也不需要当您从这样的字典创建数据框时指定列名):

    I think you're almost there, try removing the extra square brackets around the lst's (Also you don't need to specify the column names when you're creating a dataframe from a dict like this):

    import pandas as pd
    lst1 = range(100)
    lst2 = range(100)
    lst3 = range(100)
    percentile_list = pd.DataFrame(
        {'lst1Title': lst1,
         'lst2Title': lst2,
         'lst3Title': lst3
        })
    
    percentile_list
        lst1Title  lst2Title  lst3Title
    0          0         0         0
    1          1         1         1
    2          2         2         2
    3          3         3         3
    4          4         4         4
    5          5         5         5
    6          6         6         6
    ...
    

    如果您需要更高性能的解决方案,您可以在第一次尝试时使用 np.column_stack 而不是 zip,这在此处的示例中大约有 2 倍的加速,然而,在我看来,这会降低可读性:

    If you need a more performant solution you can use np.column_stack rather than zip as in your first attempt, this has around a 2x speedup on the example here, however comes at bit of a cost of readability in my opinion:

    import numpy as np
    percentile_list = pd.DataFrame(np.column_stack([lst1, lst2, lst3]), 
                                   columns=['lst1Title', 'lst2Title', 'lst3Title'])
    

    这篇关于将多个列表放入数据框中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆