Python熊猫进行循环计算

时间:2020-06-17 19:18:56

标签: python pandas for-loop

我有一个如下所示的pd数据帧(Ticker是我的索引):

enter image description here

我想计算每只股票的平均价格,以便最终输出看起来像这样:

enter image description here

这是我的代码:

average_price_output = []
for ticker in tickers:
  data[ticker] = data.loc[ticker]
  average_price = data.loc[ticker]['PX_LAST'].mean()
  average_price_output.append(round(float(average_price[-1:]), 1))

model_results = pd.DataFrame(list(zip(average_price_output)), 
               columns =['Average Price']).set_index([tickers])

还有我得到的错误:

ValueError: cannot reindex from a duplicate axis. 

问题行似乎是:data[ticker] = data.loc[ticker]

我在做什么错?提前致谢。

0 个答案:

没有答案