Reputation: 1129
I am trying to loop over my index values and perform an action A when the indexes are the same and an action B when they differ.
My index is a hash code where it is repeated across the dataframe as shown in the example below.
I can understand why I am getting the errors but I can't see how I can compare them. I don't want to use groupby() as it leads to further issues with my code (see question Python: Issue with groupby() and apply() when applying defined haversine function)
I found other questions where isin() is suggested, but this doesn't seem to apply here. What could I do?
# Unique index values
index_unique = df.index.unique().array
# All index values (including duplicates)
index_all = df.index.array
index_unique
>>
<PandasArray>
['356a192b7913b04c54574d18c28d46e6395428ab',
'da4b9237bacccdf19c0760cab7aec4a8359010b0',
'77de68daecd823babbb58edb1c8e14d7106e83bb']
Length: 3, dtype: object
index_all
>>
<PandasArray>
['356a192b7913b04c54574d18c28d46e6395428ab',
'356a192b7913b04c54574d18c28d46e6395428ab',
'356a192b7913b04c54574d18c28d46e6395428ab',
'da4b9237bacccdf19c0760cab7aec4a8359010b0',
'da4b9237bacccdf19c0760cab7aec4a8359010b0',
'77de68daecd823babbb58edb1c8e14d7106e83bb',
'77de68daecd823babbb58edb1c8e14d7106e83bb',
'77de68daecd823babbb58edb1c8e14d7106e83bb',
'77de68daecd823babbb58edb1c8e14d7106e83bb']
Length: 9, dtype: object
when trying:
while index_all == index_unique:
print("same index")
I get the error:
('Lengths must match to compare', (9,), (3,))
Same error when trying a for loop:
for i in index_all:
if index_all == index_unique:
print("same index")
else:
print("not the same index")
and when trying:
while index_all.isin(index_unique):
print("same index")
I get the error:
'PandasArray' object has no attribute 'isin'
Upvotes: 0
Views: 551
Reputation: 415
Adding it as an answer as well.
You cannot compare two arrays of different lengths directly, without accessing its elements. Try using a nested for-loop:
for i in index_all:
for j in index_unique:
if i == j:
print('Same index')
else:
print("Not the same index")
Upvotes: 2
Reputation: 47
Try the below:
def compareArray(index_unique, index_all):
matchCount = 0
for val in index_unique:
if val in index_all:
matchCount += 1
if(matchCount == len(index_unique)):
return True
else:
return False
Upvotes: 1