首页 > 其他 > 详细

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

时间:2021-09-13 19:41:53      阅读:41      评论:0      收藏:0      [点我收藏+]

一、代码

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使用

原文:https://www.cnblogs.com/zhangxianrong/p/15258001.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!