fifo_df = pd.DataFrame(columns=['Product', 'Date', 'Quantity', 'Cost'])for product in df['Product'].unique(): product_df = df[df['Product'] == product] quantity = 0 cost = 0 for index, row in product_df.iterrows(): if row['Quantity'] > 0: if quantity == 0: cost = row['Cost'] quantity = row['Quantity'] elif row['Quantity'] <= quantity: cost = ((quantity * cost) + (row['Quantity'] * row['Cost'])) / (quantity + row['Quantity']) quantity += row['Quantity'] else: cost = row['Cost'] quantity = row['Quantity'] fifo_df = fifo_df.append({'Product': row['Product'], 'Date': row['Date'], 'Quantity': row['Quantity'], 'Cost': cost}, ignore_index=True)
时间: 2023-07-15 08:10:19 浏览: 53
这段代码实现了一个先进先出(FIFO)的成本计算。首先创建一个空的DataFrame fifo_df,用于存储计算后的结果。然后遍历原始数据中所有的产品,对于每个产品,筛选出对应的数据行,接着逐行遍历数据行,计算该行的成本并添加到 fifo_df 中。
具体来说,代码首先创建一个变量 quantity 和 cost,分别表示当前所持有的产品数量和成本。然后遍历数据行,如果该行的产品数量大于0,则根据 FIFO 原则计算成本。如果当前所持有的产品数量为0,那么直接将该行的成本和数量赋值给 quantity 和 cost。如果该行的数量小于等于当前所持有的产品数量,那么将该行的成本和数量加入到当前成本的计算中,并将 quantity 加上该行的数量。如果该行的数量大于当前所持有的产品数量,那么直接将该行的成本和数量作为新的 quantity 和 cost。
最后,将每个产品的成本计算结果添加到 fifo_df 中,并返回结果。
相关问题
for product in df['Product'].unique(): product_df = df[df['Product'] == product] quantity = 0 cost = 0 for index, row in product_df.iterrows(): if row['Quantity'] > 0: if quantity == 0: cost = row['Cost'] elif row['Quantity'] <= quantity: cost = ((quantity * cost) + (row['Quantity'] * row['Cost'])) / (quantity + row['Quantity']) else: cost = row['Cost'] fifo_df = fifo_df.append({'Product': row['Product'], 'Date': row['Date'], 'Quantity': row['Quantity'], 'Cost': cost}, ignore_index=True) quantity += row['Quantity'],以上代码是否正确
这段代码存在一些问题,首先在第一次循环中,当 `quantity == 0` 时,`cost = row['Cost']`,但是并没有将 `quantity` 的值更新为当前行的 `Quantity` 值,导致后续计算成本时会出现错误。其次,`fifo_df` 在循环内部被重新赋值,每次迭代都会创建一个新的 DataFrame,这样会导致每次迭代后的结果都被覆盖,最终只会得到最后一次迭代的结果。
以下是修改后的代码:
```
fifo_df = pd.DataFrame(columns=['Product', 'Date', 'Quantity', 'Cost'])
for product in df['Product'].unique():
product_df = df[df['Product'] == product]
quantity = 0
cost = 0
for index, row in product_df.iterrows():
if row['Quantity'] > 0:
if quantity == 0:
cost = row['Cost']
quantity = row['Quantity']
elif row['Quantity'] <= quantity:
cost = ((quantity * cost) + (row['Quantity'] * row['Cost'])) / (quantity + row['Quantity'])
quantity += row['Quantity']
else:
cost = row['Cost']
quantity = row['Quantity']
fifo_df = fifo_df.append({'Product': row['Product'], 'Date': row['Date'], 'Quantity': row['Quantity'], 'Cost': cost}, ignore_index=True)
```
修改后的代码中,我们在第一次循环中添加了 `quantity = row['Quantity']` 语句,以更新 `quantity` 的值。同时,我们将 `fifo_df` 的初始化放到了循环外部,每次迭代都将新的数据添加到 DataFrame 中。在计算成本时,我们添加了一个判断条件,当 `row['Quantity'] <= quantity` 时,更新 `cost` 和 `quantity` 的值,否则直接使用当前行的 `Cost` 和 `Quantity` 更新 `cost` 和 `quantity` 的值。
Traceback (most recent call last): File "C:\Users\admin\AppData\Local\Programs\Python\Python310\1.py", line 23, in <module> fifo_df = fifo_df.append({'Product': row['Product'], 'Date': row['Date'],'Quantity': row['Quantity'], 'Cost': cost}, ignore_index=True) File "C:\Users\admin\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\generic.py", line 5989, in __getattr__ return object.__getattribute__(self, name) AttributeError: 'DataFrame' object has no attribute 'append'. Did you mean: '_append'?
这个错误提示是因为您使用的是 Pandas 1.3.0 及以上版本,而在这些版本中,Pandas 已经取消了 DataFrame 的 append 方法。您可以将多个 DataFrame 放到一个列表中,然后使用 Pandas 的 concat 方法将它们连接起来,如下所示:
```
dfs = [df1, df2, df3]
result = pd.concat(dfs)
```
在您的代码中,您可以将以下行:
```
fifo_df = fifo_df.append({'Product': row['Product'], 'Date': row['Date'], 'Quantity': row['Quantity'], 'Cost': cost}, ignore_index=True)
```
替换为以下代码:
```
new_row = {'Product': row['Product'], 'Date': row['Date'], 'Quantity': row['Quantity'], 'Cost': cost}
fifo_df = pd.concat([fifo_df, pd.DataFrame(new_row, index=[0])], ignore_index=True)
```
这样就可以避免使用 append 方法,并且代码仍然可以正确地将新行添加到 DataFrame 中。
相关推荐
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)