Search code examples
pythonpandasdataframepandas-apply

Creating multiple feature-columns using apply (or something else) on pandas column


I have a dataframe:

df = pd.DataFrame({'col1': [69, 77, 88],
                   'col2': ['barfoo', 'foo', 'bar']})
print(df)

   col1    col2
0    69  barfoo
1    77     foo
2    88     bar 

Also I have function that returns two values based on string:

def get_first_n_second(string):
    '''
    Function returns two values for two columns
    '''
    value1 = string[0]
    value2 = string[1]
    return value1, value2

And I want to create two new columns based on col2 (it doesn`t work):

df[['first', 'second']] = df['col2'].apply(get_first_n_second)

Desired output:

   col1    col2   first   second
0    69  barfoo       b        a
1    77     foo       f        o
2    88     bar       b        a

Solution

  • There are 2 changes - return Series from function:

    def get_first_n_second(string):
        '''
        Function returns two values for two columns
        '''
        value1 = string[0]
        value2 = string[1]
        return pd.Series([value1, value2])
    
    
    df[['first', 'second']] = df['col2'].apply(get_first_n_second)
    print (df)
       col1    col2 first second
    0    69  barfoo     b      a
    1    77     foo     f      o
    2    88     bar     b      a