What I want to do is save part of the dataframe into a list. I have my DataFrame data_S and when it is printed it looks like this:
data_S
open high low close volume datetime
0 329.30 334.860 327.8800 333.74 5357973.0 1.578290e+12
1 334.26 344.190 330.7100 337.28 9942277.0 1.578377e+12
2 332.40 334.030 329.6000 331.37 8246250.0 1.578463e+12
3 334.95 341.730 332.0500 336.34 8183707.0 1.578550e+12
4 335.56 337.700 329.4548 329.92 7170124.0 1.578636e+12
.. ... ... ... ... ... ...
249 216.36 218.554 214.3650 216.67 10812617.0 1.609308e+12
250 216.24 216.900 212.7000 214.06 10487567.0 1.609394e+12
251 210.00 210.200 202.4911 202.72 21225594.0 1.609740e+12
252 204.74 213.350 204.6000 211.63 19338304.0 1.609826e+12
253 210.22 215.610 209.3400 211.03 16202157.0 1.609913e+12
I want to be able to replicate the code below and change the value of the bolded with a for loop value of nums.
list_of_five_day_range = []
#so then it starts with the first list being the most recent and then in
#[X,Y,Z] Z is the most recent high or is in data_S[253]['high']
list_of_max_value = []
bars = data_S.iloc[-**5**:]['high']
list_of_five_day_range.append(list(bars))
max_value = bars.max()
list_of_max_value.append(max_value)
bars1 = data_S.iloc[**-6:-1**]['high']
list_of_five_day_range.append(list(bars1))
max_value1 = bars1.max()
list_of_max_value.append(max_value1)
max_id = bars.max()
# [X,Y,Z] Z is the most recent with the list at [0] is the most recent data
print(str(list_of_five_day_range) + " this is last 5 days of data")
# with the first number in the list is for the most recent first day high.
print(str(list_of_max_value)+" this is maxium number in last 5 days")
returns
[[218.554, 216.9, 210.2, 213.35, 215.61], [221.68, 218.554, 216.9, 210.2, 213.35]] this is last 5 days of data
[218.554, 221.68] this is maxium number in last 5 days
but in a function and for it to go through the DataFrame. This is what I have so far
def five_day_range(price_history):
for nums in range(len(price_history.index) - 1):
list_of_five_day_range = []
#so then it starts with the first list being the most recent and then [X,Y,Z] Z is the most recent
list_of_max_value = []
bars = price_history.iloc[-5 + int(-nums): int(-nums)]["high"]
list_of_five_day_range.append(bars)
max_value = bars.max()
list_of_max_value.append(max_value)
# print( str(bars)+ " this is the veyr first list of range ")
return list_of_five_day_range, list_of_max_value
However, when I print(five_day_range(data_S))
this is what I get
([0 334.86
1 344.19
Name: high, dtype: float64], [344.19])
I dont understand why it is printing this when the nums value should be going up the DataFrame. This is how I thought the for loop would go through the DataFrame. enter image description here
I thought it would first append the yellow data then the blue and green, and so on until it hits index[0]/