Reputation: 1634
I am wondering how I can apply multiple custom functions (like function1, function2) on one single column and get the results to two new columns.
import pandas as pd
# List of Tuples
matrix = [(1, 2, 3),
(4, 5, 6),
(7, 8, 9)
]
# Create a DataFrame object
df = pd.DataFrame(matrix, columns = list('xyz'),
index = list('abc'))
>>> df
x y z
a 1 2 3
b 4 5 6
c 7 8 9
>>> df_expected
x y z z_square z_divide_by_3
a 1 2 3 6 1
b 4 5 6 36 2
c 7 8 9 81 3
Upvotes: 1
Views: 50
Reputation: 262294
You can use assign
:
df.assign(z_square=df['z']**2, z_divide_by_3=df['z']/3)
output:
x y z z_square z_divide_by_3
a 1 2 3 9 1.0
b 4 5 6 36 2.0
c 7 8 9 81 3.0
Upvotes: 1