[英]Calculate percent change in a column within a group in Pandas
My dataframe has a Ticker column and a Price column.我的数据框有一个 Ticker 列和一个 Price 列。 There are 10 rows per Ticker (with the same Ticker value) but each row has a different Price.
每个 Ticker 有 10 行(具有相同的 Ticker 值),但每行都有不同的价格。
My aim is to calculate the percent change within a Ticker group.我的目标是计算 Ticker 组内的百分比变化。
I tried the following but failed:我尝试了以下但失败了:
Test_preds_actual1['Target1_preds_pct_chng'] = 0
Test_preds_actual1.groupby('Ticker')['Target1_preds_pct_chng'] =\
Test_preds_actual1.groupby('Ticker')['Target1_preds'].pct_change()
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-316-168796f850f9> in <module>
1 Test_preds_actual1.groupby('Ticker')['Target1_preds_pct_chng'] =\
----> 2 Test_preds_actual1.groupby('Ticker')['Target1_preds'].pct_change()
TypeError: 'DataFrameGroupBy' object does not support item assignment
I am not sure I understand the error message.我不确定我是否理解错误消息。 How can I accomplish my objective?
我怎样才能实现我的目标?
你可以做分配
Test_preds_actual1['Target1_preds_pct_chng']=Test_preds_actual1.groupby('Ticker')['Target1_preds'].pct_change()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.