Home > OS >  How do I access a single Pandas DataFrame element?
How do I access a single Pandas DataFrame element?

Time:04-18

I have a dataframe with three columns: date, file (filenames), and gcc (a float value). I'm trying to grab the filenames for the first, last, and middle dates. Since my dates are not in order, I found out that I can find the first and last using the min() and max(). I'm not sure how to grab the middle value, but that's not the problem. I can successfully index the first and last filenames, but I cannot figure out how to pass that indexed string to my image read function (matlibplot: imread()). Please see the code I'm using and the output below:

Generalized code:

print(df['file'][df.index[df['date'] == df['date'].min()].tolist()])

print(df.index[df['date'] == df['date'].min()].tolist())

print(df['file'][85])

Output:

85    B2AVNE_10_01_21_11_05.jpg
Name: file, dtype: object

[85]

B2AVNE_10_01_21_11_05.jpg

In the first line, I attempt to do what I want. Instead, I'm returned this Pandas object instead of the string I want.

In the second line, I ensure that the indexing code I'm using outputs the row number of the string that I want.

In the third line, I ensure that indexing the dataframe using that row number does indeed produce the string I want.

I cannot understand or find a solution to the disconnect between the first line of code and the next two lines of code.

CodePudding user response:

You can get it out like this by indexing for the specific row in the column:

df['file'].iloc[df['date'].argmin()]

EDIT:

Additional note:

df['date'].argmin() resolves to the index position of the minimum value, which is equivalent to your third attempt if your dataframe's index is ranging from 0 to n.

Also, iloc helps guarantee you are indexing based on position instead of the column's index ,which might not be in proper order or range from 0 to n (just a precautionary measure).

  • Related