If list value corresponds to column name, how to set column value to one (specified by row index)?

Viewed 39

Given a dataframe and list that looks like this:

data = {'Day':['Sunday','Monday','Tuesday','Sunday','Sunday','Monday'],
        'Apple_Kroger':[0,0,0,0,0,0],
        'Pear_Publix':[0,0,0,0,0,0],
        'Banana_Wholefoods':[0,0,0,0,0,0],
        'Kiwi_Amazon':[0,0,0,0,0,0],
        'Melon_Market':[0,0,0,0,0,0],
        'Berry_Target':[0,0,0,0,0,0]}


(output)

     Day     Apple_Kroger  Pear_Publix   Banana_Wholefoods   Kiwi_Amazon   Melon_Market  Berry_Target
0    Sunday      0             0               0                0                0           0
1    Monday      0             0               0                0                0           0
2    Tuesday     0             0               0                0                0           0
3    Sunday      0             0               0                0                0           0
4    Sunday      0             0               0                0                0           0
5    Monday      0             0               0                0                0           0


list_val = [Apple_Kroger, Kiwi_Amazon, Melon_Market]
Index = 4

How would I go about setting column values (for the specific index) that have column names the same as those in the list, to 1. Ultimately, I want the output dataframe to look like this:


     Day     Apple_Kroger  Pear_Publix   Banana_Wholefoods   Kiwi_Amazon   Melon_Market  Berry_Target
0    Sunday      0             0               0                0                0           0
1    Monday      0             0               0                0                0           0
2    Tuesday     0             0               0                0                0           0
3    Sunday      0             0               0                0                0           0
4    Sunday      1             0               0                1                1           0
5    Monday      0             0               0                0                0           0

list_val = [Apple_Kroger, Kiwi_Amazon, Melon_Market]
Index = 4

I'm having a bit of trouble at the moment utilizing df.iloc in conjunction with values in the list to apply these changes for this or any particular index value. Any help would be appreicated.

1 Answers

Like this? You can use loc to set values since you have column labels.

df.loc[index, list_val] = 1
df

       Day  Apple_Kroger  Pear_Publix  Banana_Wholefoods  Kiwi_Amazon  Melon_Market  Berry_Target
0   Sunday             0            0                  0            0             0             0
1   Monday             0            0                  0            0             0             0
2  Tuesday             0            0                  0            0             0             0
3   Sunday             0            0                  0            0             0             0
4   Sunday             1            0                  0            1             1             0
5   Monday             0            0                  0            0             0             0
Related