Pandas ValueError: Can only compare identically-labeled Series objects from single dataframe?

Question:

Ok, I thought this would be a simple way to find the rows where the value of a column was below the last value:

df.loc[df['Thing'] <= df['Thing'].tail(1)]

But I get

‘ValueError: Can only compare identically-labeled Series objects’

Researching that error message brings up causes where two dataframes are being compared, but in this case I’m using only one.

Any ideas?

Asked By: Steve Duncan

||

Answers:

df['Thing'].tail(1) returns a Series with the last value, not the last value itself. Therefore you are trying to compare (<=) two different labelled Series, df['Thing'] and df['Thing'].tail(1), but pandas doesn’t like it because it can’t align their indices to perform the comparison.

Using .iat[-1] (or .iloc[-1]) instead of .tail(1) should work:

df.loc[df['Thing'] <= df['Thing'].iat[-1]]
Answered By: Rodalm
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.