0

I have stock prices as df in this shape df

I'm trying to find to find the lower low as I move from row to row and also higher high using the following code

while i<5:
print('index value',i)
if prices_df.loc[[i],['low']] < ll:
    ll = prices_df.loc[[i],['low']]
    print('ll update to', ll, "while i=", i)
elif prices_df.loc[[i],['high']] >hh:
    hh = prices_df.loc[[i],['high']]
    print('hh update to', hh, "while i=",i)
i += 1
continue

but I got the following error

Can only compare identically-labeled DataFrame objects
ValueError: Can only compare identically-labeled DataFrame objects

saeed foroughi
  • 1,662
  • 1
  • 13
  • 25
Maged
  • 818
  • 1
  • 8
  • 17

1 Answers1

0

As pointed out by @Jon Clements. The aggregate function in pandas will do the trick.

lower_low = prices_df.head(5).agg({'low':'min'})
higher_high = prices_df.head(5).agg({'high':'max'})

df_ll = prices_df[prices_df['low'].float.contains(lower_low) == True]
df_hh = prices_df[prices_df['low'].float.contains(higher_high) == True]

Hope this helps you out.

am-3
  • 142
  • 2
  • 7
  • Thanks but I don't want to aggregate the lows. I just need to find the lowest value in the column labeled 'low'. I just need a single value not a df – Maged Jan 26 '20 at 14:48
  • You can iterate over rows in the df.head().df['column'] with a variable to store value. – am-3 Jan 26 '20 at 14:50