gpt4 book ai didi

python - 从 Pandas 中的字符串中提取 int

转载 作者:太空狗 更新时间:2023-10-29 17:21:24 24 4
gpt4 key购买 nike

假设我有一个数据框 df as

A B
1 V2
3 W42
1 S03
2 T02
3 U71

我想要一个新列(在 df 的末尾或用它替换 B 列,因为这无关紧要)只提取B 列中的 int。那就是我希望 C 列看起来像

C
2
42
3
2
71

所以如果数字前面有一个0,比如03,那么我想返回3而不是03

我该怎么做?

最佳答案

您可以转换为字符串并使用正则表达式提取整数。

df['B'].str.extract('(\d+)').astype(int)

关于python - 从 Pandas 中的字符串中提取 int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35376387/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com