[英]How to query on a pandas data frame based on column values of specific row
[英]Change row values in specific pandas data frame column with python
以下问题。
我有一个具有以下格式的数据框。
Col1 Col2 Col3 Col4
1 0 0 0 0
2 0 0 0 0
此外,我有一个与列名匹配的值列表:
ColNameList1 = [Col1, Col3] #list for the first row
ColNameList2 = [Col3, Col4] #list for the second row
目标是将每个列匹配的值0更改为1。
Col1 Col2 Col3 Col4
1 1 0 1 0
2 0 0 1 1
我对Pandas文档以及google和堆栈溢出做了一些深入的研究,但似乎没有适合这个问题的解决方案。
一如往常,任何帮助都非常感谢。
您只需使用loc和set值:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.