我正在使用nsepy模块为SBIN共享获取2周的数据。
代码:
from nsepy import get_history
from datetime import date
import pandas as pd
today = date.today()
# print(today)
sbin = get_history(symbol='SBIN', start=date(
2020, 10, 12), end=date(2020, 10, 25))
print(sbin)
#print(sbin.keys())
#sbinmax= sbin.groupby('Symbol')['High'].max
n_by_state = sbin.groupby("Symbol")["High"].count()
print(n_by_state)
The output of data.
Symbol Series Prev Close Open High Low Last Close VWAP Volume Turnover Trades Deliverable Volume %Deliverble
Date
2020-10-12 SBIN EQ 198.30 199.70 205.95 197.25 199.00 198.70 201.34 69125880 1.391764e+15 331018 9531119 0.1379
2020-10-13 SBIN EQ 198.70 198.65 200.00 195.10 195.50 195.70 197.22 34949604 6.892865e+14 182813 3878144 0.1110
2020-10-14 SBIN EQ 195.70 194.05 200.90 193.05 199.10 200.05 196.49 40550062 7.967742e+14 198041 6204348 0.1530
2020-10-15 SBIN EQ 200.05 201.00 202.50 192.00 192.80 192.85 197.72 49184944 9.724680e+14 238796 8926933 0.1815
2020-10-16 SBIN EQ 192.85 194.00 196.75 191.60 195.70 195.95 194.69 36894938 7.182954e+14 191841 3907003 0.1059
2020-10-19 SBIN EQ 195.95 196.20 204.60 196.05 204.00 204.00 201.65 60223657 1.214397e+15 323960 18100990 0.3006
2020-10-20 SBIN EQ 204.00 201.00 204.35 200.70 202.95 203.05 202.74 39317197 7.970975e+14 175146 5620943 0.1430
2020-10-21 SBIN EQ 203.05 204.70 207.30 198.85 204.00 203.75 204.08 54516482 1.112592e+15 248104 10918958 0.2003
2020-10-22 SBIN EQ 203.75 201.90 204.60 201.10 203.60 203.30 203.10 33721761 6.849009e+14 156779 4734176 0.1404
2020-10-23 SBIN EQ 203.30 204.00 205.60 201.55 202.45 202.80 203.20 32708098 6.646186e+14 143175 5408726 0.1654
Symbol
SBIN 10
我想把数据分为两周(12-16(和(19-23(,得到收盘的最高点和最低点。
谁能帮我写代码吗。
您可以为此使用resample
。它将分为两周,即10/18和10/25的一周:
sbin.index = pd.to_datetime(sbin.index)
sbin = sbin.resample('W').agg({'High': 'max', 'Low': 'min', 'Close': 'last'})
print(sbin)
High Low Close
Date
2020-10-18 205.95 191.60 195.95
2020-10-25 207.30 196.05 202.80