【问题标题】:List of LISTS of tuples to Pandas dataframe?Pandas 数据帧的元组列表列表?
【发布时间】:2019-12-21 21:54:05
【问题描述】:

我有一个元组列表列表,其中每个元组的长度相同,我需要将元组转换为 Pandas 数据帧,使数据帧的列等于元组的长度,并且每个元组项都是跨列的行条目。

我已咨询过有关此主题的其他问题(例如,Convert a list of lists of tuples to pandas dataframeList of list of tuples to pandas dataframesplit list of tuples in lists of list of tuples)未成功。

我得到的最接近的是 Stack Overflow 上另一个问题的列表理解:

import pandas as pd

tupList = [[('commentID', 'commentText', 'date'), ('123456', 'blahblahblah', '2019')], [('45678', 'hello world', '2018'), ('0', 'text', '2017')]]

# Trying list comprehension from previous stack question:
pd.DataFrame([[y for y in x] for x in tupList])

但这会产生意想不到的结果:

    0                                 1
0   (commentID, commentText, date)    (123456, blahblahblah, 2019)
1   (45678, hello world, 2018)        (0, text, 2017)

当预期结果如下:

      0            1                 2
0     commentID    commentText       date
1     123456       blahblahblah      2019
2     45678        hello world       2018
3     0            text              2017

总而言之:我需要等于每个元组长度的列(在示例中为 3),其中元组中的每个项目都是跨列的行条目。

谢谢!

【问题讨论】:

    标签: python python-3.x pandas tuples list-comprehension


    【解决方案1】:

    只需将您的列表展平为一个元组列表(您的初始列表包含一个元组的子列表):

    In [1251]: tupList = [[('commentID', 'commentText', 'date'), ('123456', 'blahblahblah', '2019')], [('45678', 'hello world', '2018'), ('0', 'text', '2017')]]
    
    In [1252]: pd.DataFrame([t for lst in tupList for t in lst])
    Out[1252]: 
               0             1     2
    0  commentID   commentText  date
    1     123456  blahblahblah  2019
    2      45678   hello world  2018
    3          0          text  2017
    

    【讨论】:

      【解决方案2】:

      更短的代码:

      from itertools import chain
      import pandas as pd
      
      tupList = [[('commentID', 'commentText', 'date'), ('123456', 'blahblahblah', '2019')], [('45678', 'hello world', '2018'), ('0', 'text', '2017')]]
      
      new_list = [x for x in chain.from_iterable(tupList)]
      df = pd.DataFrame.from_records(new_list)
      

      编辑

      您可以直接在from_records 函数中进行列表推导。

      【讨论】:

        【解决方案3】:
        tupList = [[('commentID', 'commentText', 'date'), ('123456', 'blahblahblah', '2019')], [('45678', 'hello world', '2018'), ('0', 'text', '2017')]]
        print(pd.DataFrame(sum(tupList,[])))
        

        输出

                   0             1     2
        0  commentID   commentText  date
        1     123456  blahblahblah  2019
        2      45678   hello world  2018
        3          0          text  2017
        

        【讨论】:

        • 哦,我很喜欢这个,太聪明了。希望OP接受这个,+1
        【解决方案4】:

        你可以这样做:D

        tupList = [[('commentID', 'commentText', 'date'), ('123456', 'blahblahblah', '2019')], [('45678', 'hello world', '2018'), ('0', 'text', '2017')]]
        
        # Trying list comprehension from previous stack question:
        df = pd.DataFrame([[y for y in x] for x in tupList])
        df_1 = df[0].apply(pd.Series).assign(index= range(0, df.shape[0]*2, 2)).set_index("index")
        df_2 = df[1].apply(pd.Series).assign(index= range(1, df.shape[0]*2, 2)).set_index("index")
        
        pd.concat([df_1, df_2], axis=0).sort_index()
        

        【讨论】:

        • apply(pd.Series) 老实说,这是你可以用 pandas 做的最糟糕的事情之一。太慢了。
        • 你怎么知道的?
        猜你喜欢
        • 2021-10-17
        • 1970-01-01
        • 1970-01-01
        • 2020-04-13
        • 2021-08-18
        • 1970-01-01
        • 2015-04-03
        • 2022-07-15
        • 2018-02-05
        相关资源
        最近更新 更多