python将series转换为int
时间: 2024-04-24 15:24:01 浏览: 27
要将Series转换为int类型,可以使用astype()方法或者map()函数。下面是两种方法的示例代码:
方法一:使用astype()方法
```python
import pandas as pd
s = pd.Series(data=\["001", "002", "003", "004"\], index=list("abcd"))
s = s.astype(int)
print(s)
```
运行结果:
```
a 1
b 2
c 3
d 4
dtype: int64
```
方法二:使用map()函数
```python
import pandas as pd
s = pd.Series(data=\["001", "002", "003", "004"\], index=list("abcd"))
s = s.map(int)
print(s)
```
运行结果:
```
a 1
b 2
c 3
d 4
dtype: int64
```
以上两种方法都可以将Series中的字符串类型转换为int类型。
#### 引用[.reference_title]
- *1* *3* [【Python 实战基础】Pandas中Series的创建和数据类型转换](https://blog.csdn.net/qq_39816613/article/details/126248245)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item]
- *2* [python学习教程26-转换Series的数据类型](https://blog.csdn.net/itmsn/article/details/120595509)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item]
[ .reference_list ]
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)