我看过很多类似标题的问题,但我仍然无法弄清楚。我想要做的就是将数据框中第五行第五列的值替换为值 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
is an indexer。它在索引中查找条目,但在列中查找name
不是索引。它只是一个列。以下解决方案可行:
df.loc[4, 'rating'] = 100 # Because 4 is in the index, but how do you know?
or:
df.loc[df['name']=='cheerio', 'rating'] = 100 # Find the row by column
or:
df.set_index('name', inplace=True) # Make 'name' the index
df.loc['cheerios', 'rating'] = 100 # Use the indexer
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)