You can apply
a custom python function to the column. I don't think there's a vectorized way. sum()
here takes advantage of the fact that bools are a subclass of ints so all True
values are equal to 1
.
import pandas as pd
def count_digits(string):
return sum(item.isdigit() for item in string)
df = pd.DataFrame({'a': ['thisisatest111', 'testnumber2']})
df['counts'] = df['a'].apply(count_digits)
Your approach of:
df['count_numbers'] = sum(c.isdigit() for c in df['email_alias'])
could not work because df['count_numbers'] =
is an assignment to every value in that column. Here, apply
implicitly iterates over the rows (but in Python time, so it's not vectorized). Then again, most of the .str
accessor methods of Pandas are, too, despite the syntax suggesting it will go faster than a for
loop.