元组到熊猫数据框的列表列表?

时间:2019-08-15 12:53:44

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

我有一个元组列表的列表,其中每个元组的长度都相等,我需要将元组转换为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),其中元组中的每个项目都是跨列的行条目。

谢谢!

4 个答案:

答案 0 :(得分:2)

只需将您的列表平铺成元组列表即可(您的初始列表包含元组的子列表):

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

答案 1 :(得分: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函数中进行列表理解。

答案 2 :(得分:1)

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

答案 3 :(得分:0)

您可以这样:D

 function checkDupCode() {
            var c1 = document.forms["form_ldgroup"]["coa1"].value;
            var c2 = document.forms["form_ldgroup"]["coa2"].value;
            var wrn1 = "Coa Code";
            var wrn2 = "Sudah Terpakai !";

            $.ajax({
             type: "POST",
             url: "<?php print base_url()."input/checkDupCoa"; ?>",
             data: {"hsl": c1, "hsl2": c2, "csrf_token" : $("input[name=csrf_token]").val()},
             dataType: 'json',
             success: function(hasil) { 
                if(Object.keys(hasil).length>0){
                    alert(wrn1+" "+hasil['code1']+"."+hasil['code2']+" "+wrn2); 
                }else{
                    $("#frmledger").submit();
                }   
             }
            });

            return false;
        }