0
import pandas as pd

dt = pd.DataFrame({
'name' : ['o', 'a', 'x'],
'mark' : [7, 4, 10]
})

Why does it return False when I write dt.iloc[1] is dt.iloc[1] or dt.iloc[1,2] is dt.iloc[1,2] ?

Thanks!

BeRT2me
  • 12,699
  • 2
  • 13
  • 31
  • 1
    Does this answer your question? [Understanding the "is" operator](https://stackoverflow.com/questions/13650293/understanding-the-is-operator) – Ynjxsjmh Jun 13 '22 at 17:25
  • 1
    Because those two expressions evaluate to different objects. The `is` operator tests for object identity. – juanpa.arrivillaga Jun 13 '22 at 17:25

2 Answers2

1

Use equals method to compare Series data in pandas also you can refer method Here!

import pandas as pd

dt = pd.DataFrame({
'name' : ['o', 'a', 'x'],
'mark' : [7, 4, 10]
})

dt.iloc[1].equals(dt.iloc[1])

Output:

True
Bhavya Parikh
  • 3,304
  • 2
  • 9
  • 19
1

dt.iloc[1] creates a Series that is a copy of the original, so you're asking if two different things you create are the same thing, which they are not.

If I do:

x = dt.iloc[1]
print(x is x)

y = dt.iloc[1]
print(x is y)

Output:

True
False

See Bhavya's answer for how to properly compare Series.

BeRT2me
  • 12,699
  • 2
  • 13
  • 31