Tilo
Tilo

Reputation: 429

lapply equivalent function in python

I have dataframe df with 7 entries of phone number and I want to create new renamed columns say ph1 .. ph7 and fill them with cleaned values of phone number i.e removing spaces, "/", "-", "+" etc.

With R , I can use lapply easily is there any way to do same in Python? I know do.call() can do same but facing issue coding it for same

con_1 <- con[, c("ph1", "ph2", "ph3", "ph4", "ph5", "ph6", "ph7") := 
               lapply(.SD, function(x) { gsub(paste(unlist(list(" ", "/", "-", "+")), collapse = "|"), replace = "", x) }), 
             .SDcols = c("phone1", "phone2", "phone3", "phone4", "phone5", "phone6", "phone7")]

dataframe con is:

     kac play_id     phone1     phone2 phone3 phone4    phone5 phone6 phone7
1: 5004490         20002075 0900031349 090891349   <NA>   <NA>      <NA>   <NA>   <NA>
2: 5003807         00601731       <NA>       <NA>   <NA>   <NA> 088235311   <NA>   <NA>

I need python equivalent for above

Upvotes: 7

Views: 18503

Answers (1)

Alexandre B.
Alexandre B.

Reputation: 5500

Assume that you have the following dataframe (quite different from yours since nothing will be updated in yours):

# import module
import pandas as pd

# define data frame
df = pd.DataFrame(
    [["5004490", "20002075", "09-00-03-13-49", "090891349", "", "", "", "", ""],
     ["5003807", "00601731",  "", "", "", "", "08+82+35+31/1", "", ""],
     ["5003808", "00601731",  "", "", "", "", "", "", "08/82/35/31/1"]],
    columns=['kac', 'play_id', 'phone1','phone2', 'phone3', 'phone4', 'phone5','phone6', 'phone7']
    )

# Display
print(df)
#       kac   play_id          phone1     phone2 phone3 phone4         phone5 phone6         phone7
# 0  5004490  20002075  09-00-03-13-49  090891349
# 1  5003807  00601731                                           08+82+35+31/1
# 2  5003808  00601731                                                                 08/82/35/31/1

You can define a function to apply to each cell. applymap do the job. Here I define one function clean_up_df that will remove +, - and /:

def clean_up_df(data):
    rep = data.replace('/', '')       # Replace '/' by ''
    rep = rep.replace('-', '')        # Replace '-' by ''
    rep = rep.replace('+', '')        # Replace '+' by ''
    return rep

# Columns to process
phone_columns = ['phone1', 'phone2', 'phone3',
                  'phone4', 'phone5', 'phone6', 'phone7']
# Processing the function clean_up_df
df[phone_columns] = df[phone_columns].applymap(clean_up_df)
# Display
print(df)
#        kac   play_id      phone1     phone2 phone3 phone4     phone5 phone6     phone7
# 0  5004490  20002075  0900031349  090891349
# 1  5003807  00601731                                       088235311
# 2  5003808  00601731                                                         088235311

Now, if you want to process a specific column, you can use apply with axis=1 meaning: Apply this function to each row of the dataframe. Here an example:

# column to proceed
phone_col_name = "phone1"

# Same function with the column specified
def clean_up(data):
    rep = data[phone_col_name].replace('/', '')
    rep = rep.replace('-', '')
    rep = rep.replace('+', '')
    return rep

# Process
df[phone_col_name] = df.apply(clean_up, axis=1)

# Display
print(df)
#        kac   play_id      phone1     phone2 phone3 phone4         phone5 phone6         phone7
# 0  5004490  20002075  0900031349  090891349
# 1  5003807  00601731                                       08+82+35+31/1
# 2  5003808  00601731                                                             08/82/35/31/1

Upvotes: 5

Related Questions