Pandas Python - 创建索引性能跟踪器
Pandas Python - Creating an indexed performance tracker
这是我第一次发帖,所以提前一百万感谢您对我的问题的任何解决方案!
我正在尝试创建一个函数,该函数将 fund_name 作为参数并在 fund_name df 的新列中创建索引性能计数(从 100 开始)。 df 输入类似于 this image.
并且可以在 this image 中看到所需的输出。
我使用的代码如下,但导致索引从零开始而不是 100:
def test(fund_name):
fund_name['Indexed_Performance'] = ''
fund_name['Indexed_Performance'] = pd.to_numeric(fund_name['Indexed_Performance'])
fund_name.loc[0, 'Indexed_Performance'] = fund_name.loc[0, 'Monthly_Performance']
for i in range(1, len(fund_name)):
if fund_name.loc[i, 'Indexed_Performance'] < 10:
fund_name.loc[i, 'Indexed_Performance'] = 100 * (fund_name.loc[i, 'Monthly_Performance'] + 1)
else:
fund_name.loc[i, 'Indexed_Performance'] = fund_name.loc[i-1, 'Indexed_Performance'] * (fund_name.loc[i, 'Monthly_Performance'] + 1)
再次提前感谢您对此问题的任何解决方案!
我不太清楚你的意思,尤其是在if
语句中,但也许就这么简单?
import numpy as np
def test(df):
perfs = 1 + df['Monthly_Performance']
df['Indexed_Performance'] = np.cumprod(perfs) * 100 # Cumulative product of ratios
输出
Monthly_Performance Indexed_Performance
0 -0.05 95.000000
1 0.10 104.500000
2 0.02 106.590000
3 -0.06 100.194600
4 0.00 100.194600
5 -0.04 96.186816
6 0.07 102.919893
7 -0.07 95.715501
8 -0.01 94.758346
9 0.07 101.391430
10 0.10 111.530573
11 0.07 119.337713
12 0.06 126.497976
13 -0.03 122.703036
14 0.10 134.973340
15 -0.04 129.574406
16 -0.03 125.687174
17 -0.06 118.145944
18 0.06 125.234700
如果不能,您能否提供更多有关如何计算绩效指标的详细信息?
这是我第一次发帖,所以提前一百万感谢您对我的问题的任何解决方案!
我正在尝试创建一个函数,该函数将 fund_name 作为参数并在 fund_name df 的新列中创建索引性能计数(从 100 开始)。 df 输入类似于 this image.
并且可以在 this image 中看到所需的输出。
我使用的代码如下,但导致索引从零开始而不是 100:
def test(fund_name):
fund_name['Indexed_Performance'] = ''
fund_name['Indexed_Performance'] = pd.to_numeric(fund_name['Indexed_Performance'])
fund_name.loc[0, 'Indexed_Performance'] = fund_name.loc[0, 'Monthly_Performance']
for i in range(1, len(fund_name)):
if fund_name.loc[i, 'Indexed_Performance'] < 10:
fund_name.loc[i, 'Indexed_Performance'] = 100 * (fund_name.loc[i, 'Monthly_Performance'] + 1)
else:
fund_name.loc[i, 'Indexed_Performance'] = fund_name.loc[i-1, 'Indexed_Performance'] * (fund_name.loc[i, 'Monthly_Performance'] + 1)
再次提前感谢您对此问题的任何解决方案!
我不太清楚你的意思,尤其是在if
语句中,但也许就这么简单?
import numpy as np
def test(df):
perfs = 1 + df['Monthly_Performance']
df['Indexed_Performance'] = np.cumprod(perfs) * 100 # Cumulative product of ratios
输出
Monthly_Performance Indexed_Performance
0 -0.05 95.000000
1 0.10 104.500000
2 0.02 106.590000
3 -0.06 100.194600
4 0.00 100.194600
5 -0.04 96.186816
6 0.07 102.919893
7 -0.07 95.715501
8 -0.01 94.758346
9 0.07 101.391430
10 0.10 111.530573
11 0.07 119.337713
12 0.06 126.497976
13 -0.03 122.703036
14 0.10 134.973340
15 -0.04 129.574406
16 -0.03 125.687174
17 -0.06 118.145944
18 0.06 125.234700
如果不能,您能否提供更多有关如何计算绩效指标的详细信息?