Given a pandas DataFrame with multiple columns
pd.DataFrame({'name': ['Bob', 'Alice'], 'age': [20, 40], 'height': [2.0, 2.1]})
name age height
0 Bob 20 2.0
1 Alice 40 2.1
And a function that takes multiple parameters
def example_hash(name: str, age: int) -> str:
return "In 10 years {} will be {}".format(name, age+10)
How can the DataFrame be updated with an additional column which contains the result of applying a function to a subset of the other columns?
The resulting DataFrame would be the result of applying example_hash to the name & age columns:
name age height hash
0 Bob 20 2.0 In 10 years Bob would be 30
1 Alice 40 2.1 In 10 years Alice will be 50
I'm interested in a pandas centric response. I understand that it's possible to construct a python list, iterate over the rows, and append to the list which would eventually become the column.
Thank you in advance for your consideration and response.