如何将嵌套列表作为新列添加到现有的熊猫数据框

时间:2019-02-07 06:46:36

标签: python python-3.x pandas dataframe

我创建了一个数据框“ df”,如下所示:

null

现在我有三个列表

   Name
0. School
1. Organisation 
2. Teacher 
3. Guest

我想将这4个列表添加到我现有的数据框df中,它应该看起来像这样

1. A = ['','','',['12','4']]
2. B = ['','','',['3','8']]
3. status = ['','','','[['yes','no','yes'],['no','yes','no']]]
4. letter = ['', '', '', [[['K', 'L'], ['L'], ['L']], [['O'], ['P', 'O'], ['K']]]]

我尝试了 Name A1 A2 status letter 0. School 1. Organisation 2. Teacher 3. Guest 12 3 yes K 3. Guest 12 3 yes L 3. Guest 12 3 no L 3. Guest 12 3 yes L 3. Guest 4 8 no O 3. Guest 4 8 yes P 3. Guest 4 8 yes O 3. Guest 4 8 no K df['from']=from 但这并没有像我预期的那样给我桌子。

我也尝试过:

df['to']=to

但是嵌套列表不是出现在不同的行上,而是像这样出现在同一行上

dfa=pd.DataFrame({"Names":names})

def flat(nums):
    res = []
    index = []
    for i in range(len(nums)):
        if isinstance(nums[i], list):
            res.extend(nums[i])
            index.extend([i]*len(nums[i]))
        else:
            res.append(nums[i])
            index.append(i)
    return res,index

    x="A1"
    list_flat,list_index=flat(eval(x))
    dataframe = pd.DataFrame({x:list_flat},index=list_index)
    df = pd.concat([df['Names'],dataframe],axis=1,sort=False)

    x="A2"
    list_flat,list_index=flat(eval(x))
    dataframe = pd.DataFrame({x:list_flat},index=list_index)
    df= pd.concat([df,dataframe],axis=1,sort=False)

    x="status"
    list_flat,list_index=flat(eval(x))
    dataframe = pd.DataFrame({x:list_flat},index=list_index)
    df = pd.concat([df,dataframe],axis=1,sort=False)

    x="letter"
    list_flat,list_index=flat(eval(x))
    dataframe = pd.DataFrame({x:list_flat},index=list_index)
    df = pd.concat([df,dataframe],axis=1,sort=False)

1 个答案:

答案 0 :(得分:4)

您可以尝试以下方法吗?

name = ['School', 'Organization', 'Teacher', 'Guest']
A = ['','','',['12','4']]
B = ['','','',['3','8']]
status = ['','','',[['yes','no','yes'],['no','yes','no']]]
letter = [['', '', '', [[['K', 'L'], ['L'], ['L']], [['O'], ['P', 'O'], ['K']]]]]

final_list = []
for a, b, c, d, e in zip(name, A, B, status, letter[0]):
    if any([b, c, d, e]):
        for b1, c1, d1, e1 in zip(b, c, d, e):
            for d2, e2 in zip(d1, e1):
                for e3 in e2:
                    final_list.append([a, b1, c1, d2, e3])
    else:
        final_list.append([a, b, c, d, e])

df = pd.DataFrame(final_list, columns=['Name', 'A1', 'A2', 'status', 'letter'])

输出:

            Name  A1 A2 status letter
0         School                     
1   Organization                     
2        Teacher                     
3          Guest  12  3    yes      K
4          Guest  12  3    yes      L
5          Guest  12  3     no      L
6          Guest  12  3    yes      L
7          Guest   4  8     no      O
8          Guest   4  8    yes      P
9          Guest   4  8    yes      O
10         Guest   4  8     no      K