nish nish - 1 year ago 54
Python Question

Error in dividing the columns and adding the result into a new column

I have a dataframe 'keep_df' as shown:

DateTime Modeled Flow(cfs) Observed Flow(cfs) Seconds
Event 1 2016-08-15 15:35:00 11.85926 0.0 300.00
Event 1 2016-08-15 10:05:00 30.05923 0.0 300.00
Event 1 2016-08-15 10:00:00 31.10118 0.0 300.00
Event 1 2016-08-15 09:55:00 32.17444 0.0 300.00
Event 1 2016-08-15 09:50:00 33.25405 0.0 300.00

I wanted to create new columns which is got by dividing Modeled Flow (cfs) and also Observed flow (cfs) with Seconds column as shown below:

keep_df['Modelled Volume(f3)'] = keep_df['Modeled Flow(cfs)']/keep_df['Seconds']
keep_df['Observed Volume(f3)'] = keep_df['Observed Flow(cfs)']/keep_df['Seconds']

But after running the above i'm getting an error like this:

Type error: 'Dataframe' object is not callable

Since 'Seconds' column is default value i tried this:

keep_df['Modelled Volume(f3)'] = 'Modeled Flow (cfs)'/300.00
keep_df['Observed Volume(f3)'] = 'Observed Flow (cfs)'/300.00

But still i'm getting this error:

TypeError Traceback (most recent call last)
<ipython-input-91-b268c3093f01> in <module>()
----> 1 keep_df['Modelled Volume(f3)'] = 'Modeled Flow (cfs)'/300.00
2 keep_df['Observed Volume(f3)'] = 'Observed Flow (cfs)'/300.00
3 keep_df()

TypeError: unsupported operand type(s) for /: 'str' and 'float'

What could i possibly do?

Answer Source

Try using .div instead

keep_df['Modelled Volume(f3)'] = keep_df['Modeled Flow(cfs)'].div(keep_df['Seconds'])
keep_df['Observed Volume(f3)'] = keep_df['Observed Flow(cfs)'].div(keep_df['Seconds'])

Also for the second part

keep_df['Modelled Volume(f3)'] = keep_df['Modeled Flow(cfs)']/300.00
keep_df['Observed Volume(f3)'] = keep_df['Observed Flow(cfs)']/300.00
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download