如何将其转换为输出为 CSV 的 for 循环
How to convert this to a for-loop with an output to CSV
我正在尝试将一段通用代码放在一起:
取一些价格数据的时间序列并将其分成十分位数,例如将过去 1800 万的黄金价格分成十分位数 [完成,见下文]
date 4. close decile
2017-01-03 1158.2 0
2017-01-04 1166.5 1
2017-01-05 1181.4 2
2017-01-06 1175.7 1
... ...
2018-04-23 1326.0 7
2018-04-24 1333.2 8
2018-04-25 1327.2 7
[374 rows x 2 columns]
提取特定十分位数的日期,然后创建一个额外的 30 天的辅助日期列表
#So far only for a single decile at a time
firstdecile = gold.loc[gold['decile'] == 1]
datelist = list(pd.to_datetime(firstdecile.index))
datelist2 = list(pd.to_datetime(firstdecile.index) + pd.DateOffset(months=1))
对每个十分位数取 30 天价格的平均值 returns
level1 = gold.ix[datelist]
level2 = gold.ix[datelist2]
level2.index = level2.index - pd.DateOffset(months=1)
result = pd.merge(level1,level2, how='inner', left_index=True, right_index=True)
def ret(one, two):
return (two - one)/one
pricereturns = result.apply(lambda x :ret(x['4. close_x'], x['4. close_y']), axis=1)
mean = pricereturns.mean()
Return 单个 CSV 文件中所有 10 个平均值的列表
到目前为止,我已经能够将执行步骤 1-3 的功能放在一起,但仅针对一个十分位数,但我正在努力将其扩展为同时用于所有 10 个十分位数的循环代码干净的 CSV 输出
首先将 t + 1 month
处的收盘价附加为整个数据框的新列。
gold2_close = gold.loc[gold.index + pd.DateOffset(months=1), 'close']
gold2_close.index = gold.index
gold['close+1m'] = gold2_close
但实际相关的应该是交易日数,即您不会有周末或节假日的价格。所以我建议你按行数而不是日期范围移动,即接下来的 20 个交易日
gold['close+20'] = gold['close'].shift(periods=-20)
现在计算每一行的预期值return
gold['ret'] = (gold['close+20'] - gold['close']) / gold['close']
您也可以直接合并步骤 1. 和 2.,这样您就不需要额外的列(仅当您按行数移动,而不是由于重建索引而按固定日期范围移动时)
gold['ret'] = (gold['close'].shift(periods=-20) - gold['close']) / gold['close']
因为你已经有了你的小分位数,你只需要 groupby
小分位数并将 return 与 mean()
相加
gold_grouped = gold.groupby(by="decile").mean()
输入一些随机数据,你会得到类似于下面的数据框。 close
和 ret
是每个十分位数的平均值。您可以通过 pandas.DataFrame.to_csv
从数据框中创建一个 csv
close ret
decile
0 1238.343597 -0.018290
1 1245.663315 0.023657
2 1254.073343 -0.025934
3 1195.941312 0.009938
4 1212.394511 0.002616
5 1245.961831 -0.047414
6 1200.676333 0.049512
7 1181.179956 0.059099
8 1214.438133 0.039242
9 1203.060985 0.029938
我正在尝试将一段通用代码放在一起:
取一些价格数据的时间序列并将其分成十分位数,例如将过去 1800 万的黄金价格分成十分位数 [完成,见下文]
date 4. close decile 2017-01-03 1158.2 0 2017-01-04 1166.5 1 2017-01-05 1181.4 2 2017-01-06 1175.7 1 ... ... 2018-04-23 1326.0 7 2018-04-24 1333.2 8 2018-04-25 1327.2 7 [374 rows x 2 columns]
提取特定十分位数的日期,然后创建一个额外的 30 天的辅助日期列表
#So far only for a single decile at a time firstdecile = gold.loc[gold['decile'] == 1] datelist = list(pd.to_datetime(firstdecile.index)) datelist2 = list(pd.to_datetime(firstdecile.index) + pd.DateOffset(months=1))
对每个十分位数取 30 天价格的平均值 returns
level1 = gold.ix[datelist] level2 = gold.ix[datelist2] level2.index = level2.index - pd.DateOffset(months=1) result = pd.merge(level1,level2, how='inner', left_index=True, right_index=True) def ret(one, two): return (two - one)/one pricereturns = result.apply(lambda x :ret(x['4. close_x'], x['4. close_y']), axis=1) mean = pricereturns.mean()
Return 单个 CSV 文件中所有 10 个平均值的列表
到目前为止,我已经能够将执行步骤 1-3 的功能放在一起,但仅针对一个十分位数,但我正在努力将其扩展为同时用于所有 10 个十分位数的循环代码干净的 CSV 输出
首先将
t + 1 month
处的收盘价附加为整个数据框的新列。gold2_close = gold.loc[gold.index + pd.DateOffset(months=1), 'close'] gold2_close.index = gold.index gold['close+1m'] = gold2_close
但实际相关的应该是交易日数,即您不会有周末或节假日的价格。所以我建议你按行数而不是日期范围移动,即接下来的 20 个交易日
gold['close+20'] = gold['close'].shift(periods=-20)
现在计算每一行的预期值return
gold['ret'] = (gold['close+20'] - gold['close']) / gold['close']
您也可以直接合并步骤 1. 和 2.,这样您就不需要额外的列(仅当您按行数移动,而不是由于重建索引而按固定日期范围移动时)
gold['ret'] = (gold['close'].shift(periods=-20) - gold['close']) / gold['close']
因为你已经有了你的小分位数,你只需要
相加groupby
小分位数并将 return 与mean()
gold_grouped = gold.groupby(by="decile").mean()
输入一些随机数据,你会得到类似于下面的数据框。 close
和 ret
是每个十分位数的平均值。您可以通过 pandas.DataFrame.to_csv
close ret
decile
0 1238.343597 -0.018290
1 1245.663315 0.023657
2 1254.073343 -0.025934
3 1195.941312 0.009938
4 1212.394511 0.002616
5 1245.961831 -0.047414
6 1200.676333 0.049512
7 1181.179956 0.059099
8 1214.438133 0.039242
9 1203.060985 0.029938