循环计算的Python Pandas

问题描述 投票:0回答:0

我有一个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]

我到底做错了什么?先谢谢你。

python pandas for-loop
© www.soinside.com 2019 - 2024. All rights reserved.