0

Question

How do I create multiple new DataFrame columns using DataFrame.assign and apply

Test code:

import pandas as pd
import numpy as np

df = pd.DataFrame({"Cat":1,"Dog":2},index=["10/1/21","11/1/21"])
df


       Cat  Dog
10/1/21 1   2
11/1/21 1   2

Function

def catDog (x):
    if x.Cat > x.Dog:
        sound = "Meow"
    if x.Dog > x.Cat:
        sound = "Woof"
    return (sound,sound[0])

Using apply

df2 = df.apply(catDog,axis=1, result_type='expand')
df2.columns = ["Sound","Letter"]
df2

       Sound    Letter
10/1/21 Woof    W
11/1/21 Woof    W

How do I get this desired output using something like DataFrame.assign similar to the following

       Cat  Dog Sound   Letter
10/1/21 1   2   Woof    W
11/1/21 1   2   Woof    W

What have I tried?

I can manually assign without function

df = df.assign(Sound="Woof")
df = df.assign(Letter="W")

but I want to create any number of new columns using something similar to my (toy) catDog function.

2
  • you could merge df and df2 using pd.merge or pd.concat. Or you could expand the retuned dataframe into the assign, e.g. with df.assign(**df.apply(catDog,axis=1, result_type='expand')) Commented Aug 11, 2021 at 21:02
  • Maybe df[['Sound', Letter']]= df.apply(catDog,axis=1, result_type='expand') if I'm understanding correctly and comment out df2.columns = ["Sound","Letter"]. Also catDog function could just be if else but consider if they will ever be equal and what should be returned in that case. Commented Aug 11, 2021 at 21:08

2 Answers 2

1

Try:

import pandas as pd
import numpy as np

df = pd.DataFrame({"Cat":1,"Dog":2},index=["10/1/21","11/1/21"])

def catDog (x):
    if x.Cat > x.Dog:
        sound = "Meow"
    else:
        sound = "Woof"
    return (sound,sound[0])

df[['Sound', 'Letter']] = df.apply(catDog, axis=1, result_type='expand')

print(df)

            Cat     Dog     Sound   Letter
10/1/21     1       2       Woof    W
11/1/21     1       2       Woof    W
Sign up to request clarification or add additional context in comments.

Comments

1

If you patch your fuction catDog to return a dict:

def catDog (x):
    if x.Cat > x.Dog:
        sound = "Meow"
    if x.Dog > x.Cat:
        sound = "Woof"
    d = {'Sound': sound,
         'Letter': sound[0]}
    return d

You can use join to merge your new columns:

>>> df.join(df.apply(catDog,axis=1, result_type='expand'))
         Cat  Dog Sound Letter
10/1/21    1    2  Woof      W
11/1/21    1    2  Woof      W


# or
pd.concat([df, df.apply(catDog,axis=1, result_type='expand')], axis=1)

# or
df.merge(df.apply(catDog,axis=1, result_type='expand'),
         left_index=True, right_index=True)

Comments

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.