How to get value in Dataframe given row & column values?

Hi there! Newbie here. I hope to get some help on a Python function.
Given a table (Stock Info) which has lots and lots of rows…how do I code to find the value of closing price on Nov 01, 2018?

(Attached is the first few rows of the table). Thank you!

This is a pandas question, which is a third-party package, not part of Python’s standard library. I suggest consulting a pandas-specific resource.

However, something like this should work where df is your DataFrame:

df.loc[df["Date"] == YOUR_DATE, "Close"]

The value of YOUR_DATE depends on whether date is stored as an actual datetime64 (pd.timestamp) or is a string, and if you want the scaler, you may need to get it with [0], but that’s an exercise for the reader. I encourage you to read the docs for more details. Best of luck.

Yes, date is a string so I attached quotations. Thank you so much! It worked :slight_smile:

Sir, I have a question though… What i was trying to use was .find and it doesn’t work… Is it not for a dataframe? Thank you.

As you can see from the docs and from dir(df), find is not a method defined for a DataFrame. However, you can use str.find() to search for a substring in each string in a str-format series, e.g. df["Date"].str.find(). Not sure if that’s what you want, though.

Also, you really should convert your dates to, well, dates, with pd.to_datetime(), but that’s out of scope here.

Thank you for these tips. will look into more details. just a few weeks beginning all these stuff so still clueless on various topics. Thank you!

1 Like