-1

I'm working with this dataframe. I need to find a specific index number and, based on the inspect_date, I need to find the most recent name. I tried using the following code, but it isn't working:

last_name= df.loc[[50096379],['INSPECT_DATE','NAME']].max()['INSPECT_DATE']['NAME']

By using the following, I get the most recent date, but I don't know how to then get the most recent name:

last_name= df.loc[[50096379],['INSPECT_DATE','NAME']].max()['INSPECT_DATE']

Henry Ecker
  • 34,399
  • 18
  • 41
  • 57
  • Please share all relevant code and data. See: [mcve]. It would also be good if you could explain what you’re trying to do a bit more. – AMC Nov 28 '19 at 04:28

1 Answers1

1

Run: df.loc[50096379].sort_values('INSPECT_DATE').iloc[-1].NAME

Description:

  • df.loc[...] - retrieve all rows with this key.
  • sort_values(...) - sort them by this column.
  • iloc[-1] - get the last row.
  • NAME - and from this row return the name.
Valdi_Bo
  • 30,023
  • 4
  • 23
  • 41
  • THIS WORKED!! Thank you so much. Could you explain what the iloc there is doing specifically? – Alexander Dow Nov 28 '19 at 04:36
  • *iloc* retrieves a row with the given **position** (integer, starting from 0). But as in many other places in *Python*, a negative value means "counting from the end". So argument *-1* retrieves just the last row. – Valdi_Bo Nov 28 '19 at 04:40
  • Thanks again! Was struggling for the longest time! – Alexander Dow Nov 28 '19 at 04:53