Reputation: 103
Following problem.
I have a data frame with the following format.
Col1 Col2 Col3 Col4
1 0 0 0 0
2 0 0 0 0
Furthermore, I have a List of values that match the column names:
ColNameList1 = [Col1, Col3] #list for the first row
ColNameList2 = [Col3, Col4] #list for the second row
The target is to change the value of 0 to 1 for every column row match.
Col1 Col2 Col3 Col4
1 1 0 1 0
2 0 0 1 1
I did some intense research on the Pandas documentation and also google and stack overflow but it seems there is not a fitting solution for this problem.
As always any help is much appreciated.
Upvotes: 8
Views: 41228
Reputation: 19947
You can simply use loc and set values:
df.loc[1,ColNameList1]=1
df.loc[2,ColNameList2]=1
df
Out[10]:
Col1 Col2 Col3 Col4
1 1 0 1 0
2 0 0 1 1
Upvotes: 20