. Advertisement .
..3..
. Advertisement .
..4..
Python is a famous programing language and is used mostly for software development projects. Working with Python, you will deal with a data frame, a two-dimensional data structure, which is imported from the Pandas library. Here, you might meet the error message that “Reindexing only valid with uniquely valued Index objects”. In order to understand more about this problem, let’s continue to stay tuned for our post!
When did the error “Reindexing only valid with uniquely valued Index objects” happen?
When you initialize a data frame containing many names and called “df” in the project
df:
Group Name
0 A jim
1 B bob
2 C joe
3 A jane
4 C doe
You have a data frame with schedules named “df1”
df1:
Group Due_Date Assignment
0 A 1-19 17:00 Quiz #1 A
1 B 1-21 16:30 Quiz #1 A
2 C 1-21 16:30 Quiz #1 A
5 A 1-24 11:30 Exam A
6 B 1-24 11:30 Exam A
7 C 1-24 12:30 Exam A
A df1 file has named “df1_pivoted” contains the columns of “df1” in this
df1 = df1.pivot(index='Group',
columns='Assignment',
values='Due_Date')
df1_pivoted:
Assignment Exam A Quiz #1 A
Group
A 1-24 11:30 1-19 17:00
B 1-24 11:30 1-21 16:30
C 1-24 12:30 1-21 16:30
Here, you could be faced with an error “Reindexing only valid with uniquely valued Index objects” when adding the name more.
d=df.set_index('Group')['Name']
p='({})'.format('\b|\b'.join(df['Group']))
df3['Name']=df3['Group'].str.extract(p,expand=False).map(d)
How to fix the error “Reindexing only valid with uniquely valued Index objects”
Solution 1: Privote df1 and initialize a data frame as df3
This flow happens the error: “Reindexing only valid with uniquely valued Index objects”, the final data frame you want to have is like this:
df1_final:
Group Name Exam A Quiz #1 A
A jim 1-24 11:30 1-19 17:00
B bob 1-24 11:30 1-21 16:30
C joe 1-24 12:30 1-21 16:30
A jane 1-24 11:30 1-19 17:00
C doe 1-24 12:30 1-21 16:30
As mentioned, the Pandas library is an available library in Python, used to analyze the data. This error here comes from the pandas cannot handle the name assigned. Because the added names have values even if two or more.
In the example above, let’s imagine that df1 is pivoted by df1_pivoted and a data frame as df3 has been initialized as:
df3:
Group Name
A jim
B bob
C joe
A jane
C doe
Next, the following command should do well.
df3.set_index('Group', inplace=True)
df1_pivoted = df1_pivoted.join(df3, how='left')
Besides, you can set the index again, and combine all the columns in Group.
Solution 2: Deduplicate them
Let’s follow our process below to solve the error “Reindexing only valid with uniquely valued Index objects”.
In our case, we work exactly with the sample data. We attempt to modify data in order to fix the error which is caused by duplicate column names:
df1 = pd.DataFrame({'col1': [1,2,3],
'col2': [4,5,6]
}).rename(columns={'col2':'col1'})
print (df1)
col1 col1 <- col1 is duplicated
0 1 4
1 2 5
2 3 6
df2 = pd.DataFrame({'col1': [7,8,9],
'col2': ['10','11','12'],
'col3': ['13','14','15']
})
# Concat and only maintain cols from df1
df3 = pd.concat([df1, df2], ignore_index=True).reindex(df1.columns, axis='columns')
print (df3)
You also look for them by the following command:
print (df1.columns[df1.columns.duplicated(keep=False)])
Index(['col1', 'col1'], dtype='object')
print (df2.columns[df2.columns.duplicated(keep=False)])
Index([], dtype='object')
Finally, we find that deduplicating them is the best method for us.
print (pd.io.parsers.ParserBase({'names':df1.columns})._maybe_dedup_names(df1.columns))
['col1', 'col1.1']
This solutions works very well for us and we are sure that it will work for you too. Let’s do it to get your desired result.
Conclusion
Is our post about the error “Reindexing only valid with uniquely valued Index objects” helpful to you? We hope you enjoy our post! We would appreciate it if you share your thoughts with us via the comment box below!
Thank you for reading!
Leave a comment