Python 将4d列表转换为数据帧的优化方法

Python 将4d列表转换为数据帧的优化方法,python,pandas,dataframe,Python,Pandas,Dataframe,我正在尝试将一个四维列表转换为一个数据帧。我有一个解决方案,它使用三重嵌套的for循环来实现这一点,但它非常不理想-我觉得必须有一个更快的解决方案。我一直使用的代码如下: import pandas as pd master_df = pd.DataFrame(columns=('a1', 'a2', 'intersection', 'similarity')) for i in master_list[0:2]: for x in i: for y in x:

我正在尝试将一个四维列表转换为一个数据帧。我有一个解决方案,它使用三重嵌套的
for
循环来实现这一点,但它非常不理想-我觉得必须有一个更快的解决方案。我一直使用的代码如下:

import pandas as pd

master_df = pd.DataFrame(columns=('a1', 'a2', 'intersection', 'similarity'))

for i in master_list[0:2]:
    for x in i:
        for y in x:        
            t = [y[0], y[1], repr(y[2]), y[3]]
            master_df.loc[-1] = t
            master_df.index = master_df.index + 1
            master_df = master_df.sort_index()
这是我一直试图插入到数据帧中的
主列表的一部分

master_list = [[[['residential property 42 holywell hill st. albans east of england al1 1bx',
'gnd flr 38 holywell hill st albans herts al1 1bx',
{'1bx', 'al1', 'albans', 'hill', 'holywell'},
0.5809767086589066],
['residential property 42 holywell hill st. albans east of england al1 1bx',
'62 holywell hill st albans herts al1 1bx',
{'1bx', 'al1', 'albans', 'hill', 'holywell'},
0.62250400597525191]]],
[[['aitchisons 2 holywell hill st. albans east of england al1 1bz',
'22 holywell hill st albans herts al1 1bz',
{'1bz', 'al1', 'albans', 'hill', 'holywell'},
0.64696827426453596],
['aitchisons 2 holywell hill st. albans east of england al1 1bz',
'24 holywell hill st albans herts al1 1bz',
{'1bz', 'al1', 'albans', 'hill', 'holywell'},
0.64660269146725069],
['aitchisons 2 holywell hill st. albans east of england al1 1bz',
'26 holywell hill st albans herts al1 1bz',
{'1bz', 'al1', 'albans', 'hill', 'holywell'},
0.64617599950794757],
['aitchisons 2 holywell hill st. albans east of england al1 1bz',
'20 holywell hill st albans herts al1 1bz',
{'1bz', 'al1', 'albans', 'hill', 'holywell'},
0.64798547824947428]]]]
有没有人对如何将这个4d列表转换成熊猫数据帧有什么建议。。。肾盂道

Sam

这里有一个解决方案:

  • master\u列表
  • 在字典中使用repr(我认为你真的不需要这个…)
  • 这些值必须有4列
守则:

def flatten(container):
    for i in container:
        if isinstance(i, (list,tuple)):
            for j in flatten(i):
                yield j
        else:
            yield i

def fix_dict(x):
    return repr(x) if isinstance(x, dict) else x

all_values = list(flatten(master_list))
all_values = [fix_dict(val) for val in all_values]

master_df = pd.DataFrame(np.reshape(all_values, (-1, 4)), columns = ['a1', 'a2', 'intersection', 'similarity'])
它给出了预期的输出。

这里有一个解决方案:

  • master\u列表
  • 在字典中使用repr(我认为你真的不需要这个…)
  • 这些值必须有4列
守则:

def flatten(container):
    for i in container:
        if isinstance(i, (list,tuple)):
            for j in flatten(i):
                yield j
        else:
            yield i

def fix_dict(x):
    return repr(x) if isinstance(x, dict) else x

all_values = list(flatten(master_list))
all_values = [fix_dict(val) for val in all_values]

master_df = pd.DataFrame(np.reshape(all_values, (-1, 4)), columns = ['a1', 'a2', 'intersection', 'similarity'])

这给出了预期的输出。

根据字典使用repr有什么意义?@FLab我正试图在a1和a2之间插入一组相交标记的字符串表示(如数据框列中所示)在字典中使用repr有什么意义?@FLab我正在尝试在a1和a2之间插入一组相交标记的字符串表示(如数据框列中所示)