pandas(七):列表转字典与map使用

一、代码

def clean():
    train = pd.read_csv(os.path.join(raw, "train.txt"),sep="\t", header=None,names=["s", "l"])
    train_s = train["s"].tolist()
    train_l = train["l"].tolist()

    dev = pd.read_csv(os.path.join(raw, "val.txt"),sep="\t", header=None,names=["s", "l"])
    dev_s = dev["s"].tolist()
    dev_l = dev["l"].tolist()

    test = pd.read_csv(os.path.join(raw, "test.txt"),sep="\t", header=None,names=["s", "l"])
    test_s = test["s"].tolist()
    test_l = test["l"].tolist()

    label_list = list(set(train_l + dev_l + test_l))
    label_dict = dict(zip(label_list, range(len(label_list))))
    print(label_list)
    print(label_dict)

    with open(os.path.join(data, "class.txt"), "w", encoding="utf8") as f:
        for l in label_list:
            f.write(l + "\n")

    out_train_l = list(map(lambda x: label_dict[x], train_l))
    print(out_train_l)

    out_dev_l = list(map(lambda x: label_dict[x], dev_l))
    print(out_dev_l)

    out_test_l = list(map(lambda x: label_dict[x], test_l))
    print(out_test_l)

    train_df = pd.DataFrame({"setence": train_s, "label": out_train_l})
    train_df.to_csv(os.path.join(data, "train.txt"), sep="\t", encoding="utf8", header=None, index=None)

    dev_df = pd.DataFrame({"setence": dev_s, "label": out_dev_l})
    dev_df.to_csv(os.path.join(data, "dev.txt"), sep="\t", encoding="utf8", header=None, index=None)

    test_df = pd.DataFrame({"setence": test_s, "label": out_test_l})
    test_df.to_csv(os.path.join(data, "test.txt"), sep="\t", encoding="utf8", header=None, index=None)

 

pandas(七):列表转字典与map使用

上一篇:jmeter5.2版本


下一篇:Linux目录结构01