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'?
时间: 2023-07-21 20:21:17 浏览: 47
这个错误提示是因为您使用的是 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 中。
相关问题
Traceback (most recent call last): File "C:\Users\admin\AppData\Local\Programs\Python\Python310\1.py", line 16, in <module> if row['Quantity'] > 0: TypeError: tuple indices must be integers or slices, not str
这个错误提示是在 Python 中使用了元组(tuple)而不是字典(dict)来访问一个非数字类型的索引(例如字符串)。在你的代码中,可能是因为你尝试使用了一个元组来访问一个 CSV 文件中的行数据,但是元组中的索引应该是整数或者切片,而不是字符串。
要解决这个问题,你应该使用字典而不是元组来访问 CSV 文件中的行数据。你可以使用 Python 的 csv 模块来读取 CSV 文件,并将每一行数据都转换为一个字典。例如:
```
import csv
with open('yourfile.csv', 'r') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
if int(row['Quantity']) > 0:
# do something with the row
```
在上面的代码中,我们使用 `csv.DictReader` 方法将 CSV 文件读取为一个字典序列,然后我们可以使用字典的键来访问每一行数据。由于 CSV 文件中的数据都是字符串类型,我们需要将 `row['Quantity']` 转换为整数类型才能进行比较。
Traceback (most recent call last): File "C:\Users\admin\AppData\Local\Programs\Python\Python310\2.py", line 14, in <module> writer_save() NameError: name 'writer_save' is not defined KeyboardInterrupt
这个错误提示显示 `NameError: name 'writer_save' is not defined`,意思是说在程序运行的过程中,Python 找不到名为 `writer_save` 的变量或函数。这可能是因为你在程序中调用了一个不存在的函数或变量。
另外,你的错误提示中也包含了 `KeyboardInterrupt`,这是一个异常,表示用户按下了键盘上的中断键,比如 Ctrl+C,导致程序被中断。
如果你想要解决这个问题,可以检查一下代码中是否定义了 `writer_save` 函数,并确保正确调用它。另外,你也可以在程序中添加异常处理代码,以避免程序因为用户按下中断键而意外中断。