Python is a famous programming language that has been used for designing various applications. Being a renowned one in the world of programming, every new programmer also wants to learn it. When you are working with Python, “reindexing only valid with uniquely valued Index objects” error can show up that can make you worried. There is no need to worry as we are here to guide you about the error and how it occurs.
If you ever encounter an error warning at the end of your code, then you can solve it with the method we are going to discuss in the post. But, first, understand how the error occurs
How Does “Reindexing only valid with uniquely valued Index objects” Error Occurs?
Let’s suppose you have a dataframe with the name ‘df1’
df1:
Group Name
0 A nick
1 B mike
2 C pope
3 A max
4 C chris
df1:
With a schedule dataframe named ‘df2’
df2:
Group Due_Date Assignment
0 A 1-20 18:30 Query #0 A
1 B 1-22 17:00 Query #0 A
2 C 1-22 17:00 Query #0 A
5 A 1-25 12:00 Test A
6 B 1-25 12:00 Test A
7 C 1-25 13:00 Test A
You must be pivoted df2 to keep the assignment in the column ‘df2’ put
df2 = df2.pivot(index='Group',
columns='Assignment',
values='Due_Date')
df2_pivoted:
Assignment Test A Query #0 A
Group
A 1-25 12:00 1-20 18:30
B 1-25 12:00 1-22 17:00
C 1-25 13:00 1-22 17:00
You get the error warning when you try to add the name like this:
d1=df1.set_index('Group')['Name']
p1='({})'.format('\b|\b'.join(df1['Group']))
df4['Name']=df4['Group'].str.extract(p1,expand=False).map(d1)
And here you end up with
reindexing only valid with uniquely valued Index objects
Solutions To fix ‘reindexing only valid with uniquely valued Index objects’ error
Now that you have seen how you end up with the error warning. You must be looking to have an output like this
df2_final:
Group Name Test A Query #0 A
A nick 1-25 12:00 1-20 18:30
B mike 1-25 12:00 1-22 17:00
C pope 1-25 13:00 1-22 17:00
A max 1-25 12:00 1-20 18:30
C chris 1-25 13:00 1-22 17:00
To get this in return, you need to follow the solutions below
Solution 1 – Avoid Duplicate Column names
The error can show up in the output because of the same data you are working on. Your data needs to be modified to fix the error because of the duplicate column names
df2 = pd.DataFrame({'col0': [0,1,2],
'col1': [3,4,5]
}).rename(columns={'col1':'col0'})
print (df2)
col0 col0 <- col0 is duplicated
0 0 3
1 1 4
2 2 5
df3 = pd.DataFrame({'col0': [6,7,8],
'col1': ['9','10','11'],
'col2': ['12','13','14']
})
# Join and keep columns from df2
df4 = pd.concat([df2, df3], ignore_index=True).reindex(df2.columns, axis='columns')
print (df4)
here, you land up in reindexing error. Check the below
print (df2.columns[df2.columns.duplicated(keep=False)])
Index(['col0', 'col0'], dtype='object')
print (df3.columns[df3.columns.duplicated(keep=False)])
Index([], dtype='object')
You found the issue duplication here. Now, the process of deduplicating them can solve the error.
print (pd.io.parsers.ParserBase({'names':df2.columns})._maybe_dedup_names(df2.columns))
['col0', 'col0.1']
Solution 2 – Pivot df2 and df4 with df2_pivoted
Panda is a Python Library that is known to analyze your data. The cause of the error is Panda as it is unable to handle assigned names. Let’s take a look at the example to understand it better
df4:
Group Name
A nick
B mike
C pope
A max
C chris
You should try this code to solve the issue:
df4.set_index('Group', inplace=True)
df2_pivoted = df2_pivoted.join(df4, how='left')
Conclusion
So, finally, you find the solutions to fix the “Reindexing only valid with uniquely valued Index objects” error. Hope you find it helpful!