Home > Back-end >  Get highest and lowest close price for a stock from dataframe creating user defined function
Get highest and lowest close price for a stock from dataframe creating user defined function

Time:10-25

Get highest and lowest close price of a stock.

Parameters
----------
df: Pandas DataFrame containing data from Problem 1's solution
symbol: stock symbol

Returns
-------
returns two values, highest and lowest close price of the stock.
'''

I am trying to create a user defined function which will display max and min price for selected symbol.

I have the following code but it will only display the max and min for the close column from data frame. As per the lecture I am supposed to incorporate boolean mask but i am not sure how.

def get_high_low_close(df, symbol):
    symbol = df[df.stock == "AA"]
    highest_close = df['close'].max()
    lowest_close = df['close'].min()
return highest_close, lowest_close

CodePudding user response:

Not sure what you want exactly, I'm guessing:

def get_high_low_close(df, symbol):
    mask = df['stock'] == symbol
    highest_close = df.loc[mask, 'close'].max()
    lowest_close = df.loc[mask, 'close'].min()
    return highest_close, lowest_close

See boolean indexing.

  • Related