简体   繁体   English

Python 警告

[英]Python warnings

Input:myfile输入:我的文件

在此处输入图像描述

Expected output:预期 output:

在此处输入图像描述

amountFile=pd.read_excel("myfile")
amountFile['total_amount']=amountFile.groupby('ID')["item_amount"].transform('sum')
amountFile.to_excel('file location')

above is my code to calculate new total amount column by taking sum of item amount in each item.以上是我通过计算每个项目中的项目金额总和来计算新总金额列的代码。 It works fine but I am getting warning,so I am afraid is it a problem.它工作正常,但我收到警告,所以我担心这是一个问题。

see the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy amountFile['total_amount']=amountFile['total_amount']=amountFile.groupby('ID')["item_amount"].transform('sum') C:/Users/JasnaRaghavan/PycharmProjects/PobImportScript/testReconsile.py:40: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame.请参阅文档中的警告: http://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy amountFile['total_amount']=amountFile[' total_amount']=amountFile.groupby('ID')["item_amount"].transform('sum') C:/Users/JasnaRaghavan/PycharmProjects/PobImportScript/testReconsile.py:40: SettingWithCopyWarning: 正在尝试设置值在 DataFrame 的切片副本上。 Try using.loc[row_indexer,col_indexer] = value instead尝试改用 .loc[row_indexer,col_indexer] = value

I've run your code but didn't get the same error.我已经运行了你的代码,但没有得到同样的错误。 If you're getting that warning though, it means Pandas can't guarantee whether you are working on the column itself (ie a slice of the DataFrame) or a copy of the slice.但是,如果您收到该警告,则意味着 Pandas 无法保证您是在处理列本身(即 DataFrame 的切片)还是切片的副本。

You could try using .loc to access the column instead.您可以尝试使用.loc来访问该列。 Your code would look something like this:您的代码将如下所示:

amountFile.loc[:, 'total_amount'] = amountFile.groupby('ID')["item_amount"].transform('sum')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM