简体   繁体   English

将 csv 文件第一列替换为 python 中的列表值

[英]Replace csv file first column with list values in python

I want to replace csv file first column with list values in python我想用 python 中的列表值替换 csv 文件第一列

Data:数据:

    0   1   2   3   4   5   6   7   8   9
0   0   0   0.3 0   0.3 0   0.3 0   0   0
1   0   0.2 0   0   0   0   0.2 0.4 0.2 0
2   0   0   0.2 0.1 0.3 0.1 0   0.4 0   0
3   0   0   0.1 0.2 0.1 0.1 0.2 0.1 0.1 0.1
4   0   0   0.2 0.1 0   0.1 0.2 0.2 0.2 0.1
5   0   0   0   0   0   0.3 0.3 0   0.3 0
6   0   0   0.3 0   0   0.3 0   0.3 0   0
7   0   0   0.1 0.2 0.1 0.1 0.1 0.2 0.1 0.2
8   0   0   0.1 0.1 0.1 0   0.1 0.1 0.3 0
9   0   0   0.1 0.1 0.1 0.1 0.1 0.2 0.1 0.1

Replace first column 0 1 2 3 4.... with list value.将第一列 0 1 2 3 4.... 替换为列表值。 List contain following values:列表包含以下值:

['ZINC53 (Aspirin)', 'ZINC7460 (Vatalanib)', 'ZINC1493878 (Sorafenib)', 'ZINC1546066 (Erlotinib)', 'ZINC1550477 (Lapatinib)', 'ZINC3964325 (Sunitinib)', 'ZINC13550868 (Acetaminophen)', 'ZINC19632614 (Iressa)', 'ZINC19632618 (Imatinib)', 'ZINC27439698 (Canertinib)']

I want output like this:我想要这样的 output:

    0   1   2   3   4   5   6   7   8   9
ZINC53 (Aspirin)    0   0   0.3 0   0.3 0   0.3 0   0   0
ZINC7460 (Vatalanib)    0   0.2 0   0   0   0   0.2 0.4 0.2 0
ZINC1493878 (Sorafenib) 0   0   0.2 0.1 0.3 0.1 0   0.4 0   0
ZINC1546066 (Erlotinib) 0   0   0.1 0.2 0.1 0.1 0.2 0.1 0.1 0.1
ZINC1550477 (Lapatinib) 0   0   0.2 0.1 0   0.1 0.2 0.2 0.2 0.1
ZINC3964325 (Sunitinib) 0   0   0   0   0   0.3 0.3 0   0.3 0
ZINC13550868 (Acetaminophen)    0   0   0.3 0   0   0.3 0   0.3 0   0
ZINC19632614 (Iressa)   0   0   0.1 0.2 0.1 0.1 0.1 0.2 0.1 0.2
ZINC19632618 (Imatinib) 0   0   0.1 0.1 0.1 0   0.1 0.1 0.3 0
ZINC27439698 (Canertinib)   0   0   0.1 0.1 0.1 0.1 0.1 0.2 0.1 0.1

Just use loc to modify the one column of your dataframe:只需使用loc修改您的 dataframe 的一列:

example = pd.DataFrame({0: [1, 2, 3],
                        2: ["a", "b", "c"]})

replacement_list = ["ab", "cd", "ef"]
example.loc[:, 2] = replacement_list
print(example)
   0   2
0  1  ab
1  2  cd
2  3  ef

I encourage you to look at the doc about the behaviour of loc and about indexing/selecting part of your dataframe. Of course, it will work as intended if your list has the same size of the number of rows of the dataframe. If not, you can convert your list into a series first to handle the missing data.我鼓励您查看有关loc的行为以及关于索引/选择 dataframe 的一部分的文档。当然,如果您的列表具有与 dataframe 的行数相同的大小,它将按预期工作。如果不是,您可以先将列表转换为系列以处理丢失的数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM