AttributeError: 'DataFrame' object has no attribute 'reshape'
时间: 2023-09-26 19:06:59 浏览: 596
`reshape` is a method of Numpy arrays and is not directly available in Pandas DataFrames. If you want to reshape a DataFrame, you can first convert it to a Numpy array using the `values` attribute, and then use the `reshape` method:
```python
import pandas as pd
# create a DataFrame
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]})
# convert to a Numpy array and reshape
arr = df.values.reshape((3, 3))
```
Alternatively, you can use the `stack` method to reshape the DataFrame into a Series, and then use the `values` attribute to convert it to a Numpy array:
```python
# stack the DataFrame and convert to a Numpy array
arr = df.stack().values.reshape((3, 3))
```
Note that the shape of the reshaped array should match the number of elements in the original DataFrame.
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![py](https://img-home.csdnimg.cn/images/20250102104920.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)
![](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)