Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count valid integers in a column of strings

Tags:

python

pandas

How do I count elements in a column that are valid integers?

Here is what I have been able to come up with:

import re
pd.Series(["a","2","z","123","a","oops"]).apply(lambda x: x and re.match(r"^\d+$",x) and 1).sum()
==> 2.0

and

def isint (x):
    try:
        int(x)
        return 1
    except ValueError:
        return 0

pd.Series(["a","2","z","123","a","oops"]).apply(isint).sum()
==> 2

Obviously, the second approach is better (returns in int, easily generalizes to other types - dates, floats &c), but I wonder if there is a still better way that would not require me to write my own function.

like image 927
sds Avatar asked Dec 24 '22 22:12

sds


1 Answers

The .str attribute of series offers vectorized string methods:

>>> ser = pd.Series(["a","2","z","123","a","oops"])
>>> ser.str.isdigit().sum()
2
like image 200
Mike Müller Avatar answered Jan 07 '23 00:01

Mike Müller