pandas数据类型转换
时间: 2023-10-14 09:25:59 浏览: 107
数据类型转换
Pandas数据类型转换可以通过astype()方法实现,常见的数据类型转换包括:
1. 将字符串转换为数值型数据类型(int、float)
```python
df['column_name'] = df['column_name'].astype(int)
df['column_name'] = df['column_name'].astype(float)
```
2. 将数值型数据类型(int、float)转换为字符串
```python
df['column_name'] = df['column_name'].astype(str)
```
3. 将数值型数据类型(int、float)转换为布尔型(bool)
```python
df['column_name'] = df['column_name'].astype(bool)
```
4. 将布尔型(bool)转换为数值型数据类型(int)
```python
df['column_name'] = df['column_name'].astype(int)
```
5. 将时间型数据类型(datetime)转换为字符串
```python
df['column_name'] = df['column_name'].astype(str)
```
6. 将字符串转换为时间型数据类型(datetime)
```python
df['column_name'] = pd.to_datetime(df['column_name'])
```
阅读全文