如何将数据中的树解析为Python中的嵌套列表?

时间:2017-03-05 14:44:15

标签: python dataframe tree nested

我有一些树状结构的文件。例如:

A
  Result
    a11
    a12
  Lolim
    a21
    a22
  Uplim
    a31
    a32
B
  Result
    b11
    b12
  Lolim
    b21
    b22

我有兴趣解析这些文件以获得如下所示的数据框:

Name Result Lolim Uplim
A    a12    a22   a32
B    b12    b22   NA

我的想法是将文件以某种方式分成两部分:A和B.然后在子类别中拆分每个部分。对于A,结果,Lolim和Uplim以及B结果和Lolim。最后每个子类别分为2部分。因此,我最终会得到一个嵌套列表,而且我将能够创建一个数据帧。但我不知道如何获得这个嵌套列表。

还是有其他方法吗?你能推荐一些有用的模块或功能吗?

1 个答案:

答案 0 :(得分:2)

import collections
import pandas as pd

with open("data_tree.dat", "r") as data:
    dct = collections.OrderedDict()
    key = ""
    sub_key = ""
    for line in data:
        if " " not in line:  # single space
            key = line.strip()
            dct[key] = collections.OrderedDict()
        elif " " * 4 in line and " " * 6 not in line:  # 4 spaces
            sub_key = line.strip()
            dct[key][sub_key] = ""
        elif " " * 6 in line:  # 6 spaces
            item = line.strip()
            dct[key][sub_key] = item  # overwrite, last element only

df = pd.DataFrame.from_dict(dct).transpose()
df.columns.names = ["Name"]
df = df[["Result", "Lolim", "Uplim"]]  # if column order matters
df = df.fillna("NA")  # in case you want NA and not NaN

print(df)

输出:

Name Result Lolim Uplim
A       a12   a22   a32
B       b12   b22   NA

这假设data_tree.dat看起来像this,并且包含在与包含上述代码的.py文件相同的文件夹中。

或作为一项功能:

import collections
import pandas as pd


def dat_to_df(path_to_file):
    with open(path_to_file, "r") as data:
        dct = collections.OrderedDict()
        key = ""
        sub_key = ""
        for line in data:
            if " " not in line:
                key = line.strip()
                dct[key] = collections.OrderedDict()
            elif " " * 4 in line and " " * 6 not in line:
                sub_key = line.strip()
                dct[key][sub_key] = ""
            elif " " * 6 in line:
                item = line.strip()
                dct[key][sub_key] = item

    df = pd.DataFrame.from_dict(dct).transpose()
    df.columns.names = ["Name"]
    df = df[["Result", "Lolim", "Uplim"]]
    return df.fillna("NA")

dataframe = dat_to_df("data_tree.dat")

print(dataframe)