导入数据:

import tushare as ts
df=ts.get_k_data('300038', start='2018-01-01')

print(df.head(5))

数据:

               date   open  close   high    low    volume    code
0  2018-05-14  15.57  15.57  16.88  15.57  713429.0  300038
1  2018-05-15  15.31  14.82  15.32  14.23  626289.0  300038
2  2018-05-16  14.38  15.12  15.44  14.30  640795.0  300038
3  2018-05-17  14.97  14.11  14.97  13.71  596847.0  300038
4  2018-05-18  14.20  13.66  14.34  13.51  378173.0  300038

*** 设置索引为日期序列

df = df.set_index(pd.to_datetime(df['date']))

df.drop('date', axis=1, inplace=True)

 

*** 查询上涨超过4%

print(df[(df['close'] - df['open'])/df['close'] > 0.04])

 

**** 查询上涨超过4%的日期

print(df[(df['close'] - df['open'])/df['close'] > 0.04].index)

 

*** 最后一天收盘价

print(df['close'].values[-1])

 

*** 股价分月加和,显示最后一天

print(df.resample('M').sum()) 

python笔记:金融数据、dataframe一些操作

*** 每月第一天股价

print(df.resample('MS').first()) 

*** 每年第一天股价

print(df.resample('YS').first()) 

print(df.resample('AS').first()) 

*** 每年最后一天股价

print(df.resample('Y').first()) 

print(df.resample('A').first()) 

print(df.resample('Y').first()[:-1])  #最近一年还没到最后一天,去掉

 

 

 

 

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-09-26
  • 2021-10-26
  • 2022-01-18
  • 2022-01-04
  • 2021-04-06
  • 2021-04-12
猜你喜欢
  • 2021-12-10
  • 2021-06-14
  • 2021-07-21
  • 2022-12-23
  • 2021-10-21
  • 2021-12-30
  • 2021-09-13
相关资源
相似解决方案