我看到了一堆标题相似的问题,但我还是想不通。我想要做的就是用值 100 替换第五行和第五列中我的数据框中的值。 我以为只要这样做就可以了
df.loc['cheerios','rating']= 100
因为 cheerios 是行而 rating 是列
name sugar sodium rating
0 fruit loop x x x
1 trix x x x
2 oreo x x x
3 cocoa puff x x x
4 cheerio x x 100
最佳答案
.loc
是一个索引器。它在索引中查找条目,但 name
列不是索引。它只是一个列。以下解决方案可行:
df.loc[4, 'rating'] = 100 # Because 4 is in the index, but how do you know?
或:
df.loc[df['name']=='cheerio', 'rating'] = 100 # Find the row by column
或:
df.set_index('name', inplace=True) # Make 'name' the index
df.loc['cheerios', 'rating'] = 100 # Use the indexer
关于python - 尝试更改 Pandas 数据框中的单个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50938519/