I want to replace csv file first column with list values in python
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. 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:
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
CodePudding user response:
Just use loc
to modify the one column of your 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.