geokrowding
geokrowding

Reputation: 661

perform function using values from 2 columns and return result as new column

I have a dataframe called "final", it is indexed by date (as a datetimeindex), and each column named according to the following:

final = final[['stn', 'years_of_data', 'total_minutes', 'avg_daily', 'TOA_daily']]

final.head()

           stn  years_of_data   total_minutes   avg_daily   TOA_daily
date                    
1900-01-01  AlberniElementary   4       5760    26.100  101.700
1900-01-01  AlberniWeather      6       8265    25.000  101.700
1900-01-01  Arbutus             8       11162   31.200  101.700
1900-01-01  Arrowview           7       10080   23.200  101.700
1900-01-01  Bayside             6       8597    31.600  101.700

I would like to produce/add a column of values named "AC", based on a division of "avg_daily" and "TOA_daily".

I would like the result to look like:

           stn  years_of_data   total_minutes   avg_daily   TOA_daily   AC
date                    
1900-01-01  AlberniElementary   4       5760    26.100  101.700  0.257
1900-01-01  AlberniWeather      6       8265    25.000  101.700  0.246
1900-01-01  Arbutus             8       11162   31.200  101.700  0.307
1900-01-01  Arrowview           7       10080   23.200  101.700  0.228
1900-01-01  Bayside             6       8597    31.600  101.700  0.311

I have looked into ways of creating a function to do this, but confused as to how to map and/or apply it in my case, especially the part returning the result as a new column.

Upvotes: 0

Views: 141

Answers (1)

Donbeo
Donbeo

Reputation: 17617

Try

final['AC'] = final['avg_daily']/final['TOA_daily']

which adds the desired column.

Upvotes: 2

Related Questions