1

I am running the following function but somehow struggling to have it take the length condition into account (the if part). It simply runs the first part if the function only:

stringDataFrame.apply(lambda x: x.str.replace(r'[^0-9]', '') if (len(x) >= 7) else x)

it somehow only runs the x.str.replace(r'[^0-9]', '') part for some reason, what am I doing wrong here i have been stuck.

2
  • Can you provide an example that shows your problem? Commented Aug 17, 2016 at 10:44
  • 1
    x is a series, and len(x) is the length of that series. Do you want to check individual strings' lengths? Commented Aug 17, 2016 at 10:45

1 Answer 1

1

You can use applymap when you need to work on each value separately, because apply works with all column (Series).

Then instead of using str.replace, use re.sub which works nicer for regexs:

print (stringDataFrame.applymap(lambda x: re.sub(r'[^0-9]', '', x) if (len(x) >= 7) else x))

Sample:

import pandas as pd
import re

stringDataFrame = pd.DataFrame({'A':['gdgdg454dgd','147ooo2', '123ss45678'],
                                'B':['gdgdg454dgd','x142', '12345678a'],
                                'C':['gdgdg454dgd','xx142', '12567dd8']})

print (stringDataFrame)
             A            B            C
0  gdgdg454dgd  gdgdg454dgd  gdgdg454dgd
1      147ooo2         x142        xx142
2   123ss45678    12345678a     12567dd8

print (stringDataFrame.applymap(lambda x: re.sub(r'[^0-9]', '', x) if (len(x) >= 7) else x))
          A         B       C
0       454       454     454
1      1472      x142   xx142
2  12345678  12345678  125678
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you @jezrael this works. I had tried applymap but seems like the issue was with using str.replace
Just a quick one which also falls within this question. I am horrible when it comes to lambda and functions. But i want to have two conditions by adding an additional x.contains("tel | cel | cell", case=False). This would then mean the formula should look something like this stringDataFrame.applymap(lambda x: re.sub(r'[^0-9]', '', x) if ((len(x) >= 7) & (x.contains("tel | cel | cell", case=False))) else x) @jezrael
You need plain python, try print (stringDataFrame.applymap(lambda x: re.sub(r'[^0-9]', '', x) if (len(x) >= 7) and (any(ext in x.lower() for ext in ['cel','tel','cell'])) else x))
Thanks @jazrael i will iterate through the results

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.