d0rmLife d0rmLife - 6 months ago 15
Python Question

Can a DataFrame be "reverse indexed"?

Is there a builtin function to "reverse index" a pandas data frame? For example, if the data frame represents time series data, and we iterate through the time series data, is there a builtin function to access the date for a given value?

Example:

# Let series be a pandas data frame with a column header and row labels as dates.
for i in series:
# do stuff
if something:
get date for this i


Or is it necessary to just go through the data in a different fashion?

Answer

You could do

series[series==i].index

If there can be multiple dates for the same value, you could do series[series==i].index.tolist()