简体   繁体   中英

How to apply multiple custom functions to the same column without using apply every time?

I have three functions:

def replace_date(x):
    pass

def replace_name(x):
    pass

def replace_occupation(x):
    pass 

They need to be applied to each row in a specific column. So far I write the code

df['mod'] = df['info'].apply(lambda row: replace_date(row)).apply(lambda row: replace_name(row)).apply(lambda row: replace_occupation(row)).apply(lambda row: re.sub(' +', ' ', row))

The last one I did not yet put into a separate function but I want to get rid of the three apply -s and write it in a nicer and more compact way.

Try

df['mod'] = df['info'].apply(lambda row: re.sub(' +', ' ', replace_occupation(replace_name(replace_date(row))))) 

It's one apply() although slightly less readable due to the re.sub(' +', ' ', row) in the end.

More generally, it is

df['col'].apply(lambda row: h(f(g(row))))

for some functions f() , g() , and h() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM