Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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’

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

Any ideas?

>Solution :

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]]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading