Posts

Showing posts from March 28, 2019

How to apply a function to multiple columns to create multiple columns in Pandas?

Image
0 I am trying to apply a function on multiple columns and in turn create multiple columns to count the length of each entry. Basically I have 5 columns with indexes 5,7,9,13 and 15 and each entry in those columns is a string of the form 'WrappedArray(|2008-11-12, |2008-11-12)' and in my function I try to strip the wrappedArray part and split the two values and count the (length - 1) using the following; def updates(row,num_col): strp = row[num_col.strip('WrappedAway') lis = list(strp.split(',')) return len(lis) - 1 where num_col is the index of the column and cal take the value 5,7,9,13,15. I have done this but only for 1 column: fn = lambda row: updates(row,5) col = df.apply(fn, axis=1) df = df.assign(**{'count1':col.values}) I basically want to appl