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.
The .str attribute of series offers vectorized string methods:
>>> ser = pd.Series(["a","2","z","123","a","oops"])
>>> ser.str.isdigit().sum()
2
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With