AttributeError: 'DataFrame' object has no attribute 'append'. Did you mean: '_append'?这样的错误显示怎么改正
时间: 2023-06-21 12:14:10 浏览: 3346
这个错误提示是因为pandas中的DataFrame并不具备append方法,正确的方法是使用concat或者append函数来合并两个或多个DataFrame。
使用concat方法示例:
```python
import pandas as pd
df1 = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
df2 = pd.DataFrame({'A': [7, 8, 9], 'B': [10, 11, 12]})
df3 = pd.concat([df1, df2]) # 传入一个列表,列表中包含需要合并的DataFrame对象
print(df3)
```
使用append方法示例:
```python
import pandas as pd
df1 = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
df2 = pd.DataFrame({'A': [7, 8, 9], 'B': [10, 11, 12]})
df3 = df1.append(df2) # 将df2合并到df1末尾,生成新的DataFrame对象
print(df3)
```
你可以根据自己的需求选择其中的一种方式。
相关问题
AttributeError: DataFrame object has no attribute append . Did you mean: _append ?
This error occurs when you try to call the `append` method on a Pandas DataFrame object, but the object does not have an `append` attribute.
One possible reason for this error is that you are trying to append a DataFrame to another DataFrame using the `append` method, but you are not using it correctly. In Pandas, the `append` method does not modify the original DataFrame, but instead it returns a new DataFrame that contains the rows from both DataFrames. Therefore, you need to assign the result of the `append` method to a new variable or to the original DataFrame, like this:
```
df1 = pd.DataFrame({'A': [1, 2], 'B': [3, 4]})
df2 = pd.DataFrame({'A': [5, 6], 'B': [7, 8]})
df3 = df1.append(df2)
```
In this example, `df3` will contain the rows from both `df1` and `df2`.
If you are still getting the AttributeError, it's possible that the DataFrame object you are trying to append to does not have an `append` attribute. In this case, you can try using the `_append` attribute instead, which is a private method that is used internally by Pandas. However, be aware that using private methods may not be supported in future versions of Pandas and may cause unexpected behavior.
```
df1._append(df2)
```
AttributeError: 'DataFrame' object has no attribute 'append'. Did you mean: '_append'?什么错误
这个错误是因为DataFrame对象没有名为'append'的属性或方法。相反,它有一个名为'_append'的方法。如果你想将一个DataFrame对象附加到另一个DataFrame对象的末尾,你可以使用'concat'方法。下面是一个例子:
```python
import pandas as pd
# 创建两个DataFrame对象
df1 = pd.DataFrame({'A': [1, 2], 'B': [3, 4]})
df2 = pd.DataFrame({'A': [5, 6], 'B': [7, 8]})
# 使用concat方法将它们连接起来
df3 = pd.concat([df1, df2])
# 输出结果
print(df3)
```
输出结果为:
```
A B
0 1 3
1 2 4
0 5 7
1 6 8
```
阅读全文