I have a pandas dataframe that looks like
import pandas as pd
data = {
"Race_ID": [2,2,2,2,2,5,5,5,5,5,5],
"Student_ID": [1,2,3,4,5,9,10,2,3,6,5],
"theta": [8,9,2,12,4,5,30,3,2,1,50]
}
df = pd.DataFrame(data)
and I have a function f(thetai, *theta) = thetai ** 2 + the other thetas in the same race
which I want to apply to the theta
column in dataframe grouped by Race_ID
and create a new column called feature
.
So we have
For student 1 in Race 2, the value is 8^2 + 9+2+12+4
For student 2 in Race 2, the value is 9^2 + 8+2+12+4
For student 3 in Race 2, the value is 2^2 + 8+9+12+4
etc.
I know about the groupby
and apply
methods but I don’t know how to apply the methods when the number of arguments can vary.
So the desired outcome looks like
data = {
"Race_ID": [2,2,2,2,2,5,5,5,5,5,5],
"Student_ID": [1,2,3,4,5,9,10,2,3,6,5],
"theta": [8,9,2,12,4,5,30,3,2,1,50],
"fearure": [91,107,37,167,47,111,961,97,93,91,2541]
}
df = pd.DataFrame(data)