[英]Index a pandas dataframe with multiple (row, column) pairs
I have a dataframe and want to update the values based on multiple (row, column) pairs我有一个 dataframe 并想根据多个(行、列)对更新值
test = pd.DataFrame([[1, 2, 3, 5], [4, 5, 6, 4], [6, 8, 9, 7]],
index=['a', 'b', 'c'], columns=[0,1,2,3])
For instance set pairs values at (a, 3) and (c, 1) to 1 I know that I could do it with:例如,将 (a, 3) 和 (c, 1) 处的对值设置为 1 我知道我可以这样做:
test.loc['a',3] = 1
test.loc['c',1] = 1
But I am looking for a oneliner但我正在寻找一个oneliner
If you are trying to generalize this, you can iterate of a list of tuples and set the values:如果您试图概括这一点,您可以迭代元组列表并设置值:
for r, c in [('a', 3), ('c', 1)]:
test.loc[r, c] = 1
The following might help if this is what you are looking for,如果这是您正在寻找的内容,以下内容可能会有所帮助,
test.loc[['a','c'],[3,1]] = 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.