Home > Back-end >  The column label 'national_id' is not unique
The column label 'national_id' is not unique

Time:12-29

I have two dataframe but when I use this :

total_views.columns = [['national_id','total_views']]
ret.columns = [['national_id','count']]
merged_df = ret.merge(total_views, on= 'national_id')

I got this error "The column label 'national_id' is not unique" enter image description here but when I use this one :

total_views.columns = ['national_id','total_views']
ret.columns = ['national_id','count']
merged_df = ret.merge(total_views, on= 'national_id')

my code work properly I can't find any difference between one bracket or two brackets enter image description here

I try figure out what's difference between this 2 syntaxes

CodePudding user response:

using double square brackets creates a MultiIndex for the columns which is why you get your error. If you run total_views.columns after running the lines of code to rename columns using double brackets, you get:

MultiIndex([('national_id',),
            ('total_views',)],
           )

Whereas running it after renaming the columns using single square brackets gives a normal index:

Index(['national_id', 'total_views'], dtype='object')
  • Related