我只是试图将数据数据的两列相乘,并将结果保存到这样的新列中,
df.loc[:,'sales'] = df['Quantity']*df['UnitPrice']
即使在新列中使用.loc,我仍会得到以下错误!知道我错过了什么吗?
/var/folders/qp/lp_5yt3s65q_pj__6v_kdvnh0000gn/T/ipykernel_39035/1010072081.py:3: SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead
See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
df.loc[:,'sales'] = df['Quantity']*df['UnitPrice']发布于 2022-07-06 06:05:42
我希望这对你有用:
df.assign(sales=lambda df: df.Quantity * df.UnitPrice)发布于 2022-07-06 12:35:14
谢谢你的评论。正如@Quang所提到的,这是因为我忘记使用.copy()在代码前面创建这个较小的DataFrame。
https://stackoverflow.com/questions/72877773
复制相似问题