'Renaming identical column names in Pandas [duplicate]
I have a cycle_2
df with the following column names:
3ls 3rs 3ls 3rs 3
absolute_cost 3.00 9.40 9.40 0.00 6.00
Now I need to rename them: I did the following:
cycle_2.rename(columns={cycle_2.columns[0]:'Email', cycle_2.columns[1]:'Flash', cycle_2.columns[2]:'Sms', cycle_2.columns[3]:'UPI', cycle_2.columns[4]:'IVR'})
However its printing this out:
Sms UPI Sms UPI IVR
absolute_cost 3.00 9.40 9.40 0.00 6.00
I am unable to understand why is this happeneing? How can I rename them?
Expected Output:
Email Flash Sms UPI IVR
absolute_cost 3.00 9.40 9.40 0.00 6.00
Solution 1:[1]
This is one of the possible solutions
df.columns = ['Email', 'Flash', 'Sms', 'UPI', 'IVR']
Solution 2:[2]
cycle_2.columns = ['Email', 'Flash', 'Sms', 'UPI', 'IVR']
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Maciej Fili?ski |
Solution 2 | inkarar |