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

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

问题描述

如何获取多个列表并将它们作为不同的列放在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天全站免登陆