当我运行这段代码时,我期望df2能够准确更新,但事实并非如此。这是代码...
import pandas as pd
import numpy as np
exam_data = [{'name':'Anastasia', 'score':12.5}, {'name':'Dima','score':9}, {'name':'Katherine','score':16.5}]
df = pd.DataFrame(exam_data)
df2 = df.copy()
for index, row in df.iterrows():
df2['score'] = row['score'] * 2
print(row['name'], row['score'])
print(df2)
从下面的输出中可以看到,分数没有翻倍,它们都设置为33.0
Anastasia 12.5
Dima 9.0
Katherine 16.5
name score
0 Anastasia 33.0
1 Dima 33.0
2 Katherine 33.0
这是怎么回事,为什么我会看到意外的结果?
答案 0 :(得分:0)
因为您每次迭代都设置了df2['score']
。尝试进行更改:
row['score'] = row['score'] * 2
答案 1 :(得分:0)
Pandas按栏目工作;而不是遍历行(这很慢),您可以使用
df2['score'] = df['score'] * 2
这将立即更新整列。