1

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

2 Answers 2

2

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
Sign up to request clarification or add additional context in comments.

2 Comments

Yes, i mentiond mistake about apply on full dataframe. It was accidentally.
@Warrior404 - No problem ;)
2

Alternative: use the built in str methods. It may(?) be more efficient to use the built in functions than .apply:

df['first'] = df['col2'].str[0]
df['second'] = df['col2'].str[1]

1 Comment

In this case, i think, your solution is better and easier, but there are more complex function in my real project. So str method dont have such methods. But thank you anyway.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.