How do I handle regex and null values inside a lambda expression?

Viewed 390
import pandas as pd
import re
df = pd.DataFrame({"A":['1324 cats',pd.np.nan,'There were 25647900 tickets',34254], "B":[543,678678,0,0]})

In an example DataFrame, like the one above, I am trying to calculate a new column where I extract any consecutive runs of numbers. I'm treating the cell value as a string when I hand it to re.search() in cases where there's only an integer in the cell, and I'm attempting to use pd.isna() to bypass null values, but I still get the following error: "'NoneType' object is not subscriptable"

df['C'] = df['A'].apply(lambda x: re.search(r'\d+',str(x))[0] if not pd.isna(x) else x)

This approach works fine when not involving re in the process, like this:

df['C'] = df['A'].apply(lambda x: 'Y' if not pd.isna(x) else x)

1 Answers

You can try convert the column into string type, then use str.extract (which is equivalent to re.search):

df['A'].astype(str).str.extract('(\d+)')

Output:

          0
0      1324
1       NaN
2  25647900
3     34254

Note: Your code would work fine if every single row contains a capture (some digits). re.search returns None when the pattern isn't found, so re.search(...)[0] fails and triggers that error.

Related