Omar AlHadidi
Omar AlHadidi

Reputation: 109

is operator with pandas dataframe

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!

Upvotes: 0

Views: 52

Answers (2)

BeRT2me
BeRT2me

Reputation: 13242

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.

Upvotes: 1

Bhavya Parikh
Bhavya Parikh

Reputation: 3400

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

Upvotes: 1

Related Questions