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

TKG

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 apply this function to ALL the columns (not just 5 as above) with the indexes mentioned and then create a separate column associated with columns 5,7,9,13 and 15 all in short code instead of doing that separately for each value.

I hope I made sense.

yatu

In regards to finding the amount of elements in the list, looks like you could simply use str.count() to find the amount of ',' in the strings. And in order to apply a defined function to a set of columns you could do something like:

cols = [5,7,9,13,15]

for col in cols:
    col_counts = {'{}_count'.format(col): df.iloc[:,col].apply(lambda x: x.count(','))}
    df = df.assign(**col_counts)

Alternatively you can also usestrip('WrappedAway').split(',') as you where using:

def count_elements(x):
    return len(x.strip('WrappedAway').split(',')) - 1

for col in cols:
    col_counts = {'{}_count'.format(col): 
                   df.iloc[:,col].apply(count_elements)}
    df = df.assign(**col_counts)

So for example with the following dataframe:

df = pd.DataFrame({'A': ['WrappedArray(|2008-11-12, |2008-11-12, |2008-10-11)', 'WrappedArray(|2008-11-12, |2008-11-12)'],
               'B': ['WrappedArray(|2008-11-12,|2008-11-12)', 'WrappedArray(|2008-11-12, |2008-11-12)'],
               'C': ['WrappedArray(|2008-11-12|2008-11-12)', 'WrappedArray(|2008-11-12|2008-11-12)']})

Redefining the set of columns on which we want to count the amount of elements:

for col in [0,1,2]:
    col_counts = {'{}_count'.format(col): 
                  df.iloc[:,col].apply(count_elements)}
    df = df.assign(**col_counts)

Would yield:

 A  \
0  WrappedArray(|2008-11-12, |2008-11-12, |2008-1...   
1             WrappedArray(|2008-11-12, |2008-11-12)   

                                    B  \
0   WrappedArray(|2008-11-12,|2008-11-12)   
1  WrappedArray(|2008-11-12, |2008-11-12)   

                                  C         0_count  1_count  2_count  
0  WrappedArray(|2008-11-12|2008-11-12)        2        1        0  
1  WrappedArray(|2008-11-12|2008-11-12)        1        1        0 

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

pandas apply function to multiple columns and create multiple columns to store results

How to create multiple columns using pandas apply function

How to apply a function to multiple columns to create multiple new columns in R?

pandas apply function to multiple columns with condition and create new columns

How to apply a function for multiple columns pandas?

How to apply group by function of multiple columns in pandas

How to Apply a function to multiple multiindex columns in Pandas?

Apply apply() function to multiple columns in pandas?

Apply pandas function to column to create multiple new columns error

Apply pandas function to column to create multiple new columns?

Pandas apply row-wise a function and create multiple new columns

Create multiple dynamically named pandas columns and apply a function on it

Pandas DataFrame apply function to multiple columns and output multiple columns

Apply (in Pandas) to Multiple Columns

In python pandas, How to apply loop to create rows for multiple columns?

Pandas DataFrame - How to apply Lambda Function on multiple columns and create a new column

Apply function to create string with multiple columns as argument

Apply custom function over multiple columns in pandas

Apply function to multiple pandas columns with Args

pandas groupby apply the same function to multiple columns

Apply Python function to multiple Pandas columns

Pandas update multiple columns using apply function

Apply "list" function on multiple columns pandas

Pandas apply function on dataframe over multiple columns

Pandas apply tuple unpack function on multiple columns

Update Multiple Columns using Pandas Apply Function

Pandas apply rowwise function on multiple columns

Pandas' expanding with apply function on multiple columns

How to apply lambda function on multiple columns using pandas