0

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. =)

dave
  • 33
  • 1
  • 7

1 Answers1

0

The code below works for a long time and yields the result anyway

df['Density', 'kg/m3'] = df.apply(lambda x:density(x['Temp', 'K'], x['Press','kPa']), axis=1)

The 'data' is large(ASCII, 22 Mb) but I wonder if there is a way to accelerate the calculation.

dave
  • 33
  • 1
  • 7