I want to automatically rename my dataframe with 3 name ,I tried with this code but it shows me each time only the last name :
JavaScript
x
4
1
nombres=['x','y','z']
2
for j in range(len(nombres)):
3
df.columns=[ nombres[j] +str(i) for i in range(1, len(df.columns) + 1)]
4
the result I want to achieve :
JavaScript
1
3
1
x y z x y z x y z
2
0 0 7 18 90 1 80 15 3
3
Advertisement
Answer
Can do it dynamically by multiplying the list to repeat it. Then slice by the length of the df.columns
JavaScript
1
10
10
1
import pandas as pd
2
import math
3
4
df = pd.DataFrame([[0,0,7,18,90,1,80,15,3]])
5
nombres=['x','y','z']
6
7
8
nombres = nombres * math.ceil((len(df.columns)/len(nombres)))
9
df.columns = nombres[:len(df.columns)]
10