Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如果列表值对应列名,如何将列值设置为1(由行索引指定)?_Python_Pandas_List - Fatal编程技术网

Python 如果列表值对应列名,如何将列值设置为1(由行索引指定)?

Python 如果列表值对应列名,如何将列值设置为1(由行索引指定)?,python,pandas,list,Python,Pandas,List,给定如下所示的数据帧和列表: 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],

给定如下所示的数据帧和列表:

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


     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

如何将列名与列表中列名相同的列值(针对特定索引)设置为1。最后,我希望输出数据帧如下所示:

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


     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


目前,我在使用df.iloc和列表中的值来将这些更改应用于此索引值或任何特定索引值时遇到了一些麻烦。任何帮助都会被告知。

像这样吗?您可以使用
loc
设置值,因为您有列标签

df.loc[index, list_val] = 1

节日快乐~哇!我没有意识到事情有那么简单。谢谢你直截了当的回答。@BEN_-YO祝你成为同样的朋友!