Reputation: 359
I have a dataframe, need to apply the same lambda function to multiple columns.
sample data:
col1 col2 col3
xxx;#2;yyy zzz;#46;zyzcz 1
aaa;#3;bbbccc bbbb;cccc;dd#5 2
I need to clean up and result should be as below:
col1 col2 col3
xxx;yyy zzz;zyzcz 1
aaa;bbbccc bbbb;cccc;dd 2
function I used:
def cleanDigit(row):
replacements = [('\d', ''), ('#', ''), (';;', ';')]
for (old, new) in replacements:
row = re.sub(old, new, row)
return row
code to apply function to multiple columns:
df[['col1', 'col2']] = df[['col1', 'col2']] .apply(lambda r: cleanDigit(r))
Error message:
TypeError: ('expected string or buffer', u'occurred at index col1')
Upvotes: 1
Views: 580
Reputation: 862641
Use DataFrame.applymap
, also lambda function should be omit and pass only function:
df[['col1', 'col2']] = df[['col1', 'col2']].applymap(cleanDigit)
print (df)
col1 col2 col3
0 xxx;yyy zzz;zyzcz 1
1 aaa;bbbccc bbbb;cccc;dd 2
Upvotes: 2