python中的lapply等效函数

时间:2019-05-09 10:31:18

标签: python python-3.x pandas

我有7个电话号码条目的数据框df,我想创建新的重命名列,例如ph1 .. ph7,并用电话号码的清除值填充它们,即删除空格,“ /”,“-”,“ +”等

有了R,我可以轻松地使用lapply,在Python中有什么方法可以做到这一点吗? 我知道do.call()可以做到,但面对将其编码为相同的问题

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是:

     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>

我需要以上的python

1 个答案:

答案 0 :(得分:1)

假设您具有以下数据框(与您的数据框大不相同,因为您的内容不会更新)

# 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

您可以定义要应用于每个单元格的函数。 applymap做这份工作。在这里,我定义了一个函数clean_up_df,该函数将删除+-/

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

现在,如果要处理特定列,则可以将applyaxis=1一起使用,这意味着:将此功能应用于数据框的每一行。 这里是一个例子:

# 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