将单元格设置为等于pandas中的值

时间:2016-02-11 15:04:44

标签: python pandas dataframe

我现在觉得自己像个白痴,为什么不能正常工作

results = pd.DataFrame(columns=['avg_daily_rets','cum_rets','STDEV','Sharpe'])
results.cum_rets = 4
results

返回

avg_daily_rets  cum_rets    STDEV   Sharpe

没有值

results = pd.DataFrame(columns=['avg_daily_rets','cum_rets','STDEV','Sharpe'])
results.cum_rets = np.arange(5)
results

返回

avg_daily_rets  cum_rets    STDEV   Sharpe
0   NaN 0   NaN NaN
1   NaN 1   NaN NaN
2   NaN 2   NaN NaN
3   NaN 3   NaN NaN
4   NaN 4   NaN NaN

最终我这样做了:

results = DataFrame(columns = ['avg_daily_rets','cum_rets','STDEV','Sharpe'])
results['avg_daily_rets'] = data.Daily_rets.mean()
print data.Daily_rets.mean()
results.cum_rets = data.Daily_rets.sum()
print data.Daily_rets.sum()
results.STDEV = data.Daily_rets.std()
print data.Daily_rets.std()
results.Sharpe = results.avg_daily_rets / results.STDEV * np.sqrt(250)
print results.avg_daily_rets / results.STDEV * np.sqrt(250)

0.0257851999606
46.3102191292
0.052303548452
Series([], dtype: float64)

并获得与上述相同的内容:

avg_daily_rets  cum_rets    STDEV   Sharpe

2 个答案:

答案 0 :(得分:2)

我认为您可以DataFrame而不是arrays docs创建并填充scalar

print data
   Daily_rets
a           1
b           2
c           3


avg_daily_rets = data.Daily_rets.mean()
print avg_daily_rets
2.0

cum_rets = data.Daily_rets.sum()
print cum_rets
6

STDEV = data.Daily_rets.std()
print STDEV
1.0

Sharpe = avg_daily_rets / STDEV * np.sqrt(250)
print Sharpe
31.6227766017

print pd.DataFrame( { 'avg_daily_rets': [ avg_daily_rets ],
                      'cum_rets': [ cum_rets ],
                      'STDEV': [ STDEV ],
                      'Sharpe': [ Sharpe ] })

   STDEV     Sharpe  avg_daily_rets  cum_rets
0      1  31.622777               2         6

如果您想要更改列的顺序,可以添加参数columns

print pd.DataFrame( { 'avg_daily_rets': [ avg_daily_rets ],
                      'cum_rets': [ cum_rets ],
                      'STDEV': [ STDEV ],
                      'Sharpe': [ Sharpe ] },
                    columns = ['Sharpe', 'STDEV','cum_rets','avg_daily_rets'])

      Sharpe  STDEV  cum_rets  avg_daily_rets
0  31.622777      1         6               2

答案 1 :(得分:1)

pandas中的列引用Series个对象,因此您无法将它们设置为Int

>>> import pandas as pd
>>> results = pd.DataFrame(columns=['avg_daily_rets','cum_rets','STDEV','Sharpe'])
>>> print type(results.cum_rets)
<class 'pandas.core.series.Series'>

尝试

>>>results.cum_rets = [5]
>>> print results
  avg_daily_rets  cum_rets STDEV Sharpe
0            NaN         5   NaN    NaN

Pandas会将listdict转换为Series,因此您可以从中创建列。

编辑:

我应该也指出,一旦你有了价值,你就可以直接设定个别价值。例如:

>>>results.cum_rets[0] = 6
>>>print results.cum_rets
  avg_daily_rets  cum_rets STDEV Sharpe
0            NaN         6   NaN    NaN