pandas将一列str转化为int
时间: 2023-05-31 08:18:48
浏览: 629
### 回答1:
可以使用pandas中的[ast]([http](https://geek.csdn.net/educolumn/68f28ca3131d47699e5902c5d3d74d51?spm=1055.2569.3001.10083)s://geek.csdn.net/educolumn/1d2c256574d93ae646d4473629b198e6?spm=1055.2569.3001.10083)ype()方法将一列str转化为int,示例[代码](https://geek.csdn.net/educolumn/1572ef9b473b4e00f6b2bf6d428b7c27?spm=1055.2569.3001.10083)如下:
```python
import pandas as pd
# 创建一个DataFrame
df = pd.DataFrame({'A': ['1', '2', '3', '4']})
# 将列A的数据[类型](https://geek.csdn.net/educolumn/2f8e8df4d4a5ce5581ebfcbeb16e1298?spm=1055.2569.3001.10083)从str转化为int
df['A'] = df['A'].[ast](https://geek.csdn.net/educolumn/1d2c256574d93ae646d4473629b198e6?spm=1055.2569.3001.10083)ype(int)
# 打印转化后的结果
print(df)
```