0

I have data which having duplicate column names some are different case and few are in same case. Pandas only renaming columns which are of same case while loading data to dataframe automatically. Is there is anyway to rename columns case insensitive.

Input data:

-------------------------------------------
| id  |  Name  |  class  |  class  | name |
-------------------------------------------
|  1  |  A     |   5     |   i     | W    |
|  2  |  B     |   4     |   iv    | X    |
|  3  |  C     |  10     |   x     | Y    |
|  4  |  D     |   8     |  viii   | Z    |
-------------------------------------------

Default o/p:

----------------------------------------------
| id  |  Name  |  class  |  class .1  | name |
----------------------------------------------
|  1  |  A     |   5     |   i        | W    |
|  2  |  B     |   4     |   iv       | X    |
|  3  |  C     |  10     |   x        | Y    |
|  4  |  D     |   8     |  viii      | Z    |
----------------------------------------------

Expected o/p:

-----------------------------------------------------
| id  |  Name .1  |  class .1 |  class .2  | name .2|
-----------------------------------------------------
|  1  |  A        |   5       |   i        | W      |
|  2  |  B        |   4       |   iv       | X      |
|  3  |  C        |  10       |   x        | Y      |
|  4  |  D        |   8       |  viii      | Z      |
-----------------------------------------------------
Ramineni Ravi Teja
  • 3,568
  • 26
  • 37

1 Answers1

-1

You can use:

# get lowercase name
s = df.columns.str.lower()

# group by identical names and count
suffix = df.groupby(s, axis=1).cumcount().add(1).astype(str)

# de-duplicate 
df.columns = np.where(s.duplicated(keep=False),
                      df.columns+'.'+suffix,
                      df.columns)

Output:

   id Name.1  class.1 class.2 name.2
0   1      A        5       i      W
1   2      B        4      iv      X
2   3      C       10       x      Y
3   4      D        8    viii      Z

Used input:

   id Name class class name
0   1    A     5     i    W
1   2    B     4    iv    X
2   3    C    10     x    Y
3   4    D     8  viii    Z
mozway
  • 194,879
  • 13
  • 39
  • 75