划分列并将结果添加到新列时出错

时间:2017-02-07 10:41:25

标签: python pandas

我有一个数据帧'keep_df',如下所示:

            DateTime             Modeled Flow(cfs)  Observed Flow(cfs) Seconds
   Event                
   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

我想创建新的列,这是通过将模型流(cfs)和观察流(cfs)与Seconds列分开得到的,如下所示:

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']
keep_df()

但是在运行上面之后我得到了这样的错误:

Type error: 'Dataframe' object is not callable

由于'Seconds'列是默认值,我尝试了这个:

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

但我仍然收到此错误:

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'

我能做什么?

1 个答案:

答案 0 :(得分:2)

尝试使用.div代替

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'])
keep_df

同样适用于第二部分

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