2-7 【提交作业】resample函数的应用-简答题

来源:2-7 【作业】resample函数的应用-简答题

revo09

2021-12-15

第一题:
import pandas as pd
pd.set_option(‘display.max_rows’,100000)
pd.set_option(‘display.max_columns’,10)
df = get_price(‘000001.XSHE’, start_date=‘2021-01-01’, end_date=‘2021-01-31’, frequency=‘daily’)
df_month = pd.DataFrame()
df_month[‘open’] = df[‘open’].resample(‘M’).first()
df_month[‘close’] = df[‘close’].resample(‘M’).last()
df_month[‘high’] = df[‘high’].resample(‘M’).max()
df_month[‘low’] = df[‘low’].resample(‘M’).min()
df_month[‘volume’] = df[‘volume’].resample(‘M’).sum()
df_month[‘money’] = df[‘money’].resample(‘M’).sum()
print(df_month)
输入结果:
open close high low volume money
2021-01-31 18.95 22.91 23.36 17.66 2.814472e+09 5.860380e+10

第二题:
import pandas as pd
pd.set_option(‘display.max_rows’,100000)
pd.set_option(‘display.max_columns’,10)
df = get_price(‘000001.XSHE’, start_date=‘2020-01-01’, end_date=‘2020-12-31’, frequency=‘daily’)
df_month = pd.DataFrame()
df_month[‘open’] = df[‘open’].resample(‘M’).first()
df_month[‘close’] = df[‘close’].resample(‘M’).last()
df_month[‘high’] = df[‘high’].resample(‘M’).max()
df_month[‘low’] = df[‘low’].resample(‘M’).min()
df_month[‘volume’] = df[‘volume’].resample(‘M’).sum()
df_month[‘money’] = df[‘money’].resample(‘M’).sum()
print(df_month)
输出结果:
open close high low volume money
2020-01-31 16.24 15.16 16.91 15.01 1.520850e+09 2.466329e+10
2020-02-29 13.65 14.14 15.33 13.65 2.595982e+09 3.750110e+10
2020-03-31 14.19 12.49 15.26 11.62 2.725267e+09 3.669330e+10
2020-04-30 12.54 13.59 13.97 12.16 1.750160e+09 2.244271e+10
2020-05-31 13.42 12.90 13.78 12.45 1.244672e+09 1.631273e+10
2020-06-30 13.00 12.70 13.77 12.42 1.841689e+09 2.378542e+10
2020-07-31 12.69 13.24 16.50 12.64 4.845076e+09 6.941590e+10
2020-08-31 13.36 14.96 15.56 13.33 3.148977e+09 4.516316e+10
2020-09-30 14.84 15.05 15.96 14.49 2.168742e+09 3.292620e+10
2020-10-31 15.18 17.61 18.63 15.01 2.100316e+09 3.589953e+10
2020-11-30 17.51 19.59 20.72 16.56 2.073678e+09 3.779005e+10
2020-12-31 19.55 19.19 20.35 17.65 1.977047e+09 3.692006e+10

写回答

1回答

DeltaF

2021-12-15

棒棒哒,祝你学习愉快

0
0

程序员理财课 Python量化交易系统实战

打造一个自动交易平台,新手也能提升理财收益

1987 学习 · 405 问题

查看课程