AttributeError: 'DataFrame' object has no attribute 'dtype'. Did you mean: 'dtypes'?
时间: 2023-08-02 12:12:00 浏览: 76
Yes, the error message "AttributeError: 'DataFrame' object has no attribute 'dtype'" suggests that you are trying to access the "dtype" attribute of a DataFrame object in Python, but it does not exist.
The correct attribute to access the data types of columns in a pandas DataFrame is "dtypes" (with an "s" at the end). So, you can try using `df.dtypes` instead of `df.dtype`.
For example:
```python
import pandas as pd
# Create a DataFrame
df = pd.DataFrame({'A': [1, 2, 3], 'B': ['a', 'b', 'c']})
# Access the data types of columns
print(df.dtypes)
```
This will print the data types of each column in the DataFrame.
相关问题
AttributeError: 'DataFrame' object has no attribute 'ftypes'. Did you mean: 'dtypes'?
这个错误是因为在代码中使用了`ftypes`属性,但是`DataFrame`对象没有`ftypes`属性。正确的属性应该是`dtypes`。你可以将代码中的`ftypes`改为`dtypes`来解决这个错误。
以下是修改后的代码示例:
```python
columns = df.columns.tolist()
field = [] # 用来接收字段名称的列表
table = [] # 用来接收字段名称和字段类型的列表
types = df.dtypes
print(types)
for col in columns:
if 'int' in str(df[col].dtype):
char = col + ' INT'
elif 'float' in str(df[col].dtype):
char = col + ' FLOAT'
elif 'object' in str(df[col].dtype):
char = col + ' VARCHAR(255)'
elif 'datetime' in str(df[col].dtype):
char = col + ' DATETIME'
else:
char = col + ' VARCHAR(255)'
table.append(char)
field.append(col)
```
AttributeError: 'DataFrame' object has no attribute 'dt'. Did you mean: 'at'?
这个错误通常是因为DataFrame对象没有dt属性,但是你尝试访问了它。正确的属性应该是dtypes,它返回一个Series,其中包含DataFrame中每列的数据类型。你可以使用以下代码来查看DataFrame中每列的数据类型:
```python
import pandas as pd
data = pd.read_excel(r"C:\Users\ASUS\Desktop\0012\data7.1.2.xlsx")
print(data.dtypes)
```
如果你想查看特定列的数据类型,可以使用以下代码:
```python
print(data['score'].dtype)
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![py](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)