pandas 从字符串中提取数字 [英] Pandas Extract Number from String
本文介绍了 pandas 从字符串中提取数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
给出以下数据框:
import pandas as pd
import numpy as np
df = pd.DataFrame({'A':['1a',np.nan,'10a','100b','0b'],
})
df
A
0 1a
1 NaN
2 10a
3 100b
4 0b
我想从每个单元格(存在的地方)中提取数字. 理想的结果是:
I'd like to extract the numbers from each cell (where they exist). The desired result is:
A
0 1
1 NaN
2 10
3 100
4 0
我知道可以用str.extract
完成,但是我不确定如何.
I know it can be done with str.extract
, but I'm not sure how.
推荐答案
给它一个正则表达式捕获组:
Give it a regex capture group:
df.A.str.extract('(\d+)')
给你:
0 1
1 NaN
2 10
3 100
4 0
Name: A, dtype: object
这篇关于 pandas 从字符串中提取数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文