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

Time:08-21

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?

CodePudding user response:

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]]
  • Related