从许多列表中创建数据框

时间:2020-06-26 07:14:24

标签: python pandas

我想从我的数据创建数据框。我要做的实际上是对我的算法的不同参数进行网格搜索。您是否知道如何做得更好,因为现在如果我需要在网格中添加两个以上的参数,或者添加要执行分析的更多数据,则需要手动添加很多列表,然后追加给它一些值,然后在Dataframe dict中添加另一列。还有其他方法吗?因为现在看起来真的很丑。

type_preds = []
type_models = []
type_lens = []
type_smalls = []
lfc1s = []
lfc2s = []
lfc3s = []
lv2s = []
sfp1s = []
len_small_fils = []
ratio_small_fills = []
ratio_big_fils = []
for path_to_config in path_to_results.iterdir():
    try:
        type_pred, type_model, type_len, type_small, len_small_fil, ratio_big_fil, ratio_small_fill = path_to_config.name[:-4].split('__')
    except:
        print(path_to_config)
        continue
    path_to_trackings = sorted([str(el) for el in list(path_to_config.iterdir())])[::-1]

    sfp1, lv2, lfc3, lfc2, lfc1 = display_metrics(path_to_gts, path_to_trackings)
    type_preds.append(type_pred)
    type_models.append(type_model)
    type_lens.append(type_len)
    type_smalls.append(type_small)
    len_small_fils.append(len_small_fil)
    ratio_big_fils.append(ratio_big_fil)
    ratio_small_fills.append(ratio_small_fill)
    lfc1s.append(lfc1)
    lfc2s.append(lfc2)
    lfc3s.append(lfc3)
    lv2s.append(lv2)
    sfp1s.append(sfp1)

df = pd.DataFrame({
    'type_pred': type_preds,
    'type_model': type_models,
    'type_len': type_lens,
    'type_small': type_smalls,
    'len_small_fil': len_small_fils,
    'ratio_small_fill': ratio_small_fills,
    'ratio_big_fill': ratio_big_fils,
    'lfc3': lfc3s,
    'lfc2': lfc2s,
    'lfc1': lfc1s,
    'lv2': lv2s,
    'sfp1': sfp1s
})

1 个答案:

答案 0 :(得分:0)

遵循这些思路可能会更容易:

data = []

for path_to_config in path_to_results.iterdir():
    row = []
    try:
        row.extend(path_to_config.name[:-4].split('__'))
    except:
        print(path_to_config)
        continue
    path_to_trackings = sorted([str(el) for el in list(path_to_config.iterdir())])[::-1]
    row.extend(display_metrics(path_to_gts, path_to_trackings))
    data.append(row)

df = pd.DataFrame(
    data,
    columns=[
        "type_pred",
        "type_model",
        "type_len",
        "type_small",
        "len_small_fil",
        "ratio_big_fil",
        "ratio_small_fill",
        # End of first half
        "sfp1",
        "lv2",
        "lfc3",
        "lfc2",
        "lfc1",
    ])

然后,每当您向第一个或第二个函数添加额外的返回变量时,您只需要在最终的DataFrame创建中添加额外的列即可。