1

How to pass all the column from dataframe into different user defined function

here is mine dataframe look like

    data = [['tom', 10, 9876765143, 'SUN 1023'], ['nick', 15, 98767654312, 'SUN 1023'], ['juli', 14, 98769876541, 'SUN 1023']]    
    df = pd.DataFrame(data, columns = ['Name', 'Age', 'Number', 'Address'])         
    df 

here are function right now i am just showing one function

        def number(inp):
            import re
            regex = r'^\s*(?:\+?(\d{1,3}))?[-. (]*(\d{3})[-. )]*(\d{3})[-. ]*(\d{4})(?: *x(\d+))?\s*$'
            inp = inp.replace(regex, 'XXXXXXX')
            print (inp)

  number(df.Number)

It throws error

     ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

similarly i have multiple function i just want to pass each column from dataframe with the value associated within the column of dataframe into each user defined function like i have a function number similarly i have other function def new() def beg() one by one. Is there any way to solve that problem

4
  • I run your code and see no error, the result is: 0 9876765143 1 98767654312 2 98769876541 Commented Nov 12, 2019 at 19:23
  • 0 1 and 2 are indexes, 0:9876765143 1:98767654312 2:98769876541 Commented Nov 12, 2019 at 19:27
  • i want to use it in a function and i have several other regex too even i have to apply multiple regex to a single column. I just want to pass each column into all the user defined function one by one is it possible? Commented Nov 12, 2019 at 19:33
  • I am not sure if i fully understand what you want, you have more than one function and every function is designed to apply regrex to specific column of the dataframe,is that right? Commented Nov 12, 2019 at 19:51

1 Answer 1

2

Why even use a function? You can directly apply the regex pattern to the relevant column(s).

regex = r'^\s*(?:\+?(\d{1,3}))?[-. (]*(\d{3})[-. )]*(\d{3})[-. ]*(\d{4})(?: *x(\d+))?\s*$'

>>> df['Number'].astype(str).str.replace(regex, 'XXXXXXX')
0    XXXXXXX
1    XXXXXXX
2    XXXXXXX
Name: Number, dtype: object

If you need a function:

import random

def phone_number(series):
    regex = r'^\s*(?:\+?(\d{1,3}))?[-. (]*(\d{3})[-. )]*(\d{3})[-. ]*(\d{4})(?: *x(\d+))?\s*$'
    return series.str.replace(regex, 'X' * random.randrange(3, 8))

>>> phone_number(df['Number'].astype(str))
0    XXXXXXX
1    XXXXXXX
2    XXXXXXX
Name: Number, dtype: object
Sign up to request clarification or add additional context in comments.

6 Comments

Hey @Alexander i want to use it in a function and i have several other regex too even i have to apply multiple regex to a single column. I just want to pass each column into all the user defined function one by one is it possible?
How to iterate all such function to each and every column in dataframe
Can i generate random length 'XXXXX'
To apply it to all columns in the dataframe: {col: phone_number(df[col].astype(str)) for col in df}. For specific columns, just replace df with a list of the relevant columns. You could generate random length XXXXX, but that sounds like a separate question.
As i think i just need to use randint to generate random lenghth 'XXXXX' if you could answer me that will be become easy for me thanks anyway
|

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.