假设,excel中有一个列,其值如下...仅存在 5500 个数字,但显示长度 5602 意味着存在 102 个字符串
4 SELECTIO
6 N NO
14 37001
26 37002
38 37003
47 37004
60 37005
73 37006
82 37007
92 37008
105 37009
119 37010
132 37011
143 37012
157 37013
168 37014
184 37015
196 37016
207 37017
220 37018
236 37019
253 37020
267 37021
280 37022
287 Krishan
290 37023
300 37024
316 37025
337 37026
365 37027
...
74141 42471
74154 42472
74169 42473
74184 42474
74200 42475
74216 42476
74233 42477
74242 42478
74256 42479
74271 42480
74290 42481
74309 42482
74323 42483
74336 42484
74350 42485
74365 42486
74378 42487
74389 42488
74398 42489
74413 42490
74430 42491
74446 42492
74459 42493
74474 42494
74491 42495
74504 42496
74516 42497
74530 42498
74544 42499
74558 42500
Name: Selection No., Length: 5602, dtype: object
我只想使用 pandas 在 python 中获取这样的数值
37001
37002
37003
37004
37005
我该怎么做?我已使用 pandas 在 python 中附加了我的代码......................................... ......
def selection(sle):
if sle in re.match('[3-4][0-9]{4}',sle):
return 1
else:
return 0
select['status'] = select['Selection No.'].apply(selection)
现在我收到一个“‘NoneType’类型的参数不可迭代”
错误。
最佳答案
尝试将 Numpy 与 np.isreal 一起使用,并且仅选择数字..
import pandas as pd
import numpy as np
df = pd.DataFrame({'SELECTIO':['N NO',37002,37003,'Krishan',37004,'singh',37005], 'some_col':[4,6,14,26,38,47,60]})
df
SELECTIO some_col
0 N NO 4
1 37002 6
2 37003 14
3 Krishan 26
4 37004 38
5 singh 47
6 37005 60
>>> df[df[['SELECTIO']].applymap(np.isreal).all(1)]
SELECTIO some_col
1 37002 6
2 37003 14
4 37004 38
6 37005 60
结果:
特定于列SELECTIO
..
df[df[['SELECTIO']].applymap(np.isreal).all(1)]
SELECTIO some_col
1 37002 6
2 37003 14
4 37004 38
6 37005 60
或者只是导入numbers
+ lambda
的另一种方法:
import numbers
df[df[['SELECTIO']].applymap(lambda x: isinstance(x, numbers.Number)).all(1)]
SELECTIO some_col
1 37002 6
2 37003 14
4 37004 38
6 37005 60
注意: 提取正在使用的列时出现问题 ['Selection No.']
但确实如此你的名字中有一个空格,它会像 ['选择号']
这就是你得到的原因 KeyError
在执行时,尝试看看!
关于python - 如何从该列中仅提取数字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53800443/