从退货PANDAS创建并编制索引 [英] Create and index from returns PANDAS
本文介绍了从退货PANDAS创建并编制索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下数据框,其中前两列是月报表(您必须乘以100才能得到%)
I have the following dataframe where the first two columns are monthly returns (you have to multiply by 100 to get it in %)
in_scenario_USA out_scenario_USA USA
month year
6 1960 0.000000 0.019524 0.0
7 1960 0.000000 -0.024772 0.0
8 1960 0.000000 0.026121 0.0
9 1960 0.000000 -0.060393 0.0
10 1960 0.000000 -0.002429 0.0
11 1960 0.000000 0.040270 0.0
12 1960 0.000000 0.046273 0.0
1 1961 0.000000 0.063156 0.0
2 1961 0.026870 0.000000 1.0
3 1961 0.025536 0.000000 1.0
... ... ...
4 2017 0.000000 0.009091 0.0
5 2017 0.000000 0.011576 0.0
6 2017 0.000000 0.004814 0.0
7 2017 0.019349 0.000000 1.0
8 2017 0.000546 0.000000 1.0
我希望创建两个索引,对于USA列,第一个值非NaN将从1开始,然后基本上是:-第一个索引将基于第一列的返回-第二个索引将基于第二列的返回值
I wish to create two indices that would start at 1 for the first value non NaN for the USA column and then basically: - The first index will be based on the return of the first column - The second index will be based on the returns of the second column
例如第一个会是这样
in_scenario_USA first index
month year
6 1960 0.000000 1
7 1960 0.000000 1
8 1960 0.000000 1
9 1960 0.000000 1
10 1960 0.000000 1
11 1960 0.000000 1
12 1960 0.000000 1
1 1961 0.000000 1
2 1961 0.026870 1.026870
3 1961 0.025536 1.053092
4 1961 0.003843 1.057139
5 1961 0.000000 1.057139
6 1961 0.000000 1.057139
我有点想有效地进行迭代.
I am kind of struggling about the way to iterate that in an efficient way.
推荐答案
我认为应该是这样的:
df['First index'] = df.in_scenario_USA.cumsum().add(1)
这篇关于从退货PANDAS创建并编制索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文