尽管我使用的是df.loc,但我尝试在Jupyter环境中运行此代码,但得到“试图在DataFrame的切片副本上设置一个值”。
对于以下代码,如何避免出现此错误消息?我要做的就是舍入“得分”列中的值。
import pandas as pd
import numpy as np
data = [['Orange',11.111],['Orange',12.1111111],['Orange',13.111111],
['Orange',14.11111],['Orange',15.111111],['Orange',16.111111],
['Orange',17.11111],['Orange',18.11111],
['StrawBerry',22.11111],['StrawBerry',23.11111],
['StrawBerry',24.11111],
['StrawBerry',25.11111],['StrawBerry',26.11111],
['StrawBerry',27.111111]]
df = pd.DataFrame(data,columns=['Fruit', 'Score'])
df
df['Score'] = df.loc[:,'Score'].apply(lambda x: np.round(x,decimals=4))
print (df['Score'])
print (df)