0

When I use the DataFrame.assign() method in my own function foobar, it has no effect to the global DataFrame.

#!/usr/bin/env python3
import pandas as pd

def foobar(df):
    # has no affect to the "global" df
    df.assign(Z = lambda x: x.A + x.B)

    return df    
    

data = {'A': range(3),
        'B': range(3)}

df = pd.DataFrame(data)
df = foobar(df)
# There is no 'Z' column in this df
print(df)

The result output

   A  B                                                                                          
0  0  0                                                                                          
1  1  1                                                                                          
2  2  2 

I assume this has something to do with the difference of views and copy's in Pandas. But I am not sure how to handle this the right and elegant Pandas-way.

1
  • 3
    You need to assign it back i.e., df = df.assign(Z=lambda x: x.A + x.B) or it's effectively doing nothing. Commented Jun 21, 2021 at 10:52

1 Answer 1

3

Pandas assign returns a DataFrame so you need to assign the result to the same df. Try this:

def foobar(df):
    df = df.assign(Z = lambda x: x.A + x.B)

    return df
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much. It really was a misunderstanding based on assumptions grounded in the object orientation, not a typo.

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.