Python – Adds a value to the Pandas index

Adds a value to the Pandas index… here is a solution to the problem.

Adds a value to the Pandas index

I

have 2 data frames and I want to merge them without duplicating columns :

First, I only get unique columns from the second data frame (like answer in this):

cols_to_use = df2.columns.difference(df1)

I need to keep a column, which is my unique identifier, named complete_name

Now, cols_to_use doesn’t contain complete_name because it’s marked as a duplicate, how do I add it? I tried:

cols_to_use.append(pd. Index(['complete_name']))

But it does nothing. Does it help?

Solution

Works well for me :

df1 = pd. DataFrame({'A':list('abcdef'),
                   'B':[4,5,4,5,5,4],
                   'C':[7,8,9,4,2,3],
                   'D':[1,3,5,7,1,0],
                   'E':[5,3,6,9,2,4],
                   'F':list('aaabbb')})

cols_to_use = df1.columns.difference(['A','C'])
print (cols_to_use)
Index(['B', 'D', 'E', 'F'], dtype='object')

print (cols_to_use.append(pd. Index(['complete_name'])))
Index(['B', 'D', 'E', 'F', 'complete_name'], dtype='object')

print (cols_to_use.union(['complete_name']))
Index(['B', 'D', 'E', 'F', 'complete_name'], dtype='object')

Related Problems and Solutions