dermen
dermen

Reputation: 5382

How to rename columns while overwriting potential duplicates in pandas dataframe

I have a pandas.dataframe:

import pandas as pd
df = pd.DataFrame( {'one': pd.Series([1., 2., 3.], 
                                     index=['a', 'b', 'c']),
                    'two': pd.Series([1., 2., 3., 4.], 
                                     index=['a', 'b', 'c', 'd']),
                    'three': pd.Series([0., 6., 1.], 
                                     index=['b', 'c', 'd']),
                    'two_': pd.Series([1., 2., 5, 4.], 
                                     index=['a', 'b', 'c', 'd'])})

or

print (df) 
#   one  three  two  two_
#a    1    NaN    1     1
#b    2      0    2     2
#c    3      6    3     5
#d  NaN      1    4     4

and I have a map which renames certain columns as such

name_map = {'one': 'one', 'two': 'two_'} 
df.rename(columns=name_map)
#    one  three  two_  two_
# a    1    NaN     1     1
# b    2      0     2     2
# c    3      6     3     5
# d  NaN      1     4     4

(occasionally name_map might map a column to itself, e.g. 'one' -> 'one'). What I want in the end is the object

#    one_  three  two_ 
#a     1    NaN      1    
#b     2      0      2    
#c     3      6      3    
#d   NaN      1      4        

How I should remove potential duplicates before renaming?

Upvotes: 3

Views: 1430

Answers (3)

fixxxer
fixxxer

Reputation: 16174

I think the easiest way would be to drop the columns which are not present in the name_map values list (since you want to remove the first two column)

In [74]: df
Out[74]: 
   one  two  two_
a    1    1     1
b    2    2     2
c    3    3     5
d  NaN    4     4

In [76]: df.drop([col for col in df.columns if col not in name_map.keys()], axis=1)
Out[76]: 
   one  two
a    1    1
b    2    2
c    3    3
d  NaN    4

Upvotes: 0

Zero
Zero

Reputation: 77027

First get the common columns list(set(name_map.values()) & set(df.columns)) and drop() them. And, then rename() it using columns=name_map

In [16]: (df.drop(list(set(name_map.values()) & set(df.columns)), axis=1)
            .rename(columns=name_map))
Out[16]:
   one_  two_
a     1     1
b     2     2
c     3     3
d   NaN     4

Upvotes: 3

dermen
dermen

Reputation: 5382

I have one method, but it seems a bit messy (dealing with the NaN values contributes to the messiness)

potential_duplicates = [ new 
                         for old,new in name_map.items() 
                         if new in list(df) # if the new column name exists
                         and 
                         pd.np.any( df[old][df[old]==df[old]]  # if said column differs from the one to be renames 
                                     != df[new][df[new]==df[new]] ) ]

df.drop( potential_duplicates, axis = 1, inplace=True)

df.rename( columns=name_map) 

#    one_  two_ 
#a     1     1
#b     2     2
#c     3     3
#d   NaN     4

Upvotes: 0

Related Questions