【问题标题】:How to update the values of a column at specific row and column using pandas [closed]如何使用熊猫更新特定行和列的列值[关闭]
【发布时间】:2021-07-05 15:38:52
【问题描述】:

我正在尝试更新特定行和列的值

这是我想要做的:

import pandas as pd

df = pd.DataFrame([[0, 2, 3], [0, 4, 1], [10, 20, 30]],
                  index=[4, 5, 6], columns=['A', 'B', 'C'])

df.at(4,'A')=10 #trying to update the value of row 4 and column A

我收到如下错误,我无法弄清楚可能是什么问题

df.at(4,'A')=10
^
SyntaxError: can't assign to function call

我必须使用 .at() 来满足我的其他要求。 任何帮助/建议都非常感谢,谢谢!

【问题讨论】:

  • 您需要重复您的 PANDAS 使用教程。 Stack Overflow 并不打算取代现有的文档和教程。请从intro tour 重复on topichow to ask
  • 好一个@Prune!如果我知道我做错了什么,我会更高兴。
  • 总是有助于检查docs
  • 如我所说,请查看教程以了解正确用法。这与 Stack Overflow 无关。

标签: python python-3.x pandas dataframe


【解决方案1】:

使用方括号而不是圆括号:

df.at[4,'A'] = 10

【讨论】:

  • 不走运! df.loc(4,'A')=10 - SyntaxError: can't assign to function call
  • 我再次更新了我的答案,而不是圆括号!
  • 那行得通,如果可能的话,请告诉我为什么 .at 不起作用。我正在努力学习..
  • .at 正在工作,您使用了圆括号,它与方括号一起工作正常,如我的回答所示。 () 是一个函数调用。 .at 不是函数,而是索引技术。
猜你喜欢
  • 2016-03-26
  • 2018-08-08
  • 1970-01-01
  • 2019-04-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-04
  • 2015-10-23
相关资源
最近更新 更多