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.
CodePudding user response:
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()
.