I have a df with a number or columns, I would like to rename these with incremental numbers selecting the starting and ending range.
With the above image, I would like to select only columns B-D, and rename them to 1-4. So the resulting data frame would be:
So basically selecting the headers via index numbers and adding incremental numbers instead.
EDIT: The above dataframe
data = [['a','b','c','d','e','f'], ['a','b','c','d','e','f'], ['a','b','c','d','e','f'],['a','b','c','d','e','f']]
df = pd.DataFrame(data, columns = ['A','B','C','D','E','F'])
>Solution :
Use rename with selected columns by DataFrame.loc – here between B and E:
c = df.loc[:, 'B':'E'].columns
df = df.rename(columns=dict(zip(c, range(1, len(c) + 1))))
print (df)
A 1 2 3 4 F
0 a b c d e f
1 a b c d e f
2 a b c d e f
3 a b c d e f

