I'd like to create a new column by the return values of a function that requires two columns as arguments.
Here is my dataframe(df). the data frame has multi-column index
Time | Temp | Press |
---|---|---|
sec | K | kPa |
0.00 | 300 | 101 |
.... | .... | .... |
4.00 | 380 | 180 |
And I have a function returning a new value from the Temp and Press
def density(Temp, Press)
....
return rho
With this function, I'd like to create a new column as below
Time | Temp | Press | Density |
---|---|---|---|
sec | K | kPa | kg/m3 |
0.00 | 300 | 101 | 1000 |
.... | .... | .... | .... |
4.00 | 380 | 180 | 1004 |
From my own searching, it seems that I need to use 'apply'.
So I tried some codes like
df['Density', 'kg/m3'] = df['Density', 'kg/m3'].apply(density, args=(df['Temp', 'K'], df['Press','kPa']))
And I got the error message
TypeError: 'module' object is not callable
From a QnA, I guess the function definition shall be modified to have dataframe type as arguments. But I'm stuck here.
It would be solved if I use another method like for-iteration. But I expect that there is a faster and neat expression for this problem.
Is there a solution for this?
I appreciate it in advance. =)