2

I'm new to Python, my problem is I have two dataframes and I need to compare if the last row of the two are the same, both dataframes are the same size (have the same columns).

A B C D     A B C D
1 1 1 1     1 1 1 1
2 2 2 2     2 3 3 3

I was thinking of something like

if df_csv.tail(1) != df.tail(1):
 print ('Not equal')
else:
 print ('Equal')

but trows this error

ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

  • You are almost there! Use `(df_csv.tail(1) != df.tail(1)).any()` – rafaelc Oct 16 '19 at 01:29
  • 1
    `df_csv.tail(1) != df.tail(1)` yields a Series. So in your example of one row being `[2,2,2,2]` and the other being `[2,3,3,3]` your `if` statement becomes `if [False, True, True, True]`, which "makes no sense". The `.any()` at the end checks if there is _at least one_ `True` value in that series of True/False values. If there is at least one `True`, it means that at least one value differs between these two rows and therefore they're not equal – rafaelc Oct 16 '19 at 01:31

0 Answers0