'Renaming columns in pandas [duplicate]
Could someone please help me with renaming columns? I have a large dataset with about 150 columns and I need to rename each of the columns. I've been looking for the answer for the past 2 hours and I just can't find anything. I know how to use the basic function 'rename', but I'm pretty sure there is an easier and quicker way to do it than just renaming each columns one by one? Any help would be really appreciated! Thanks :)
Solution 1:[1]
You can just rename it directly, like:
df.columns = ['first', 'second', 'third']
In your case you can use:
df.columns = ['Special{i}'.format(i=i) for i in range(0, len(df.columns))]
Solution 2:[2]
Try:
df.columns = 'Special' + pd.RangeIndex(1, len(df.columns)+1).astype(str)
print(df.columns)
# Output
Index(['Special1', 'Special2', 'Special3', 'Special4', 'Special5', 'Special6',
'Special7'],
dtype='object')
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 | |
| Solution 2 |
