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 |
-----------------------------------------------------
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.