简体   繁体   English

如何按索引替换 pandas dataframe 中的单个值

[英]How to replace single value in pandas dataframe by index

Below is an example of what I want to do.下面是我想做的一个例子。 Basically I know the index and the column of the value I want to replace and just want to replace a specific cell.基本上我知道要替换的值的索引和列,并且只想替换特定的单元格。 No conditional statements.没有条件语句。


df_a = pd.DataFrame(data={"a": [0, 0, 0], "b": [0, 0, 0]})
display(df_a)

    a   b
0   0   0
1   0   0
2   0   0

df_b = pd.DataFrame(data={"a": [0, 0, 10], "b": [0, 0, 0]})

    a   b
0   0   0
1   0   0
2   10  0

I was getting false positives on answer until I found what I wanted on documenting:在我找到我想要记录的内容之前,我的回答一直是误报:

df_a = pd.DataFrame(data={"a": [0, 0, 0], "b": [0, 0, 0]})


df_a.at[2, "b"] = 10
display(df_a)

    a   b
0   0   0
1   0   0
2   10  0

NOTE: python df_a.iat[2,1] can be used to achieve the same result if wanting to use integer-based column indexes.注意:如果想要使用基于整数的列索引,可以使用python df_a.iat[2,1]来实现相同的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM