I really love the pandas.assign()
function, especially in combination with the lambda
expression.
However, I ran into an unknown behavior when dealing with string concatenation that I don't understand yet. I have found this thread, but it does not answer my question:
String concatenation of two pandas columns
Minimal working example of my problem:
import pandas as pd
df = pd.DataFrame({'Firstname': ['Sandy', 'Peter', 'Dolly'],
'Surname': ['Sunshine', 'Parker', 'Dumb']})
which returns
Firstname Surname
0 Sandy Sunshine
1 Peter Parker
2 Dolly Dumb
Now, if I'd like to assign e.g. Full Name
I thought I could simply do:
df = df.assign(**{'Full Name': lambda x: f'{x.Firstname} {x.Surname}'})
but this does not just create a new string like "Sandy Sunshine" based on each row (as expected) but on all rows like this:
Could anyone explain me why my approach does not work and why this
df = df.assign(**{'Full Name': lambda x: x.Firstname + ' ' + x.Surname})
obviously works? Thank you :)