# Change the name of columns based on a period in df

## Change the name of columns based on a period in df

Contents

Problem Description:

I have a df and I want to change the name of column based on a period. For example, in the following df, I have 15 columns with name v0-14. I want to rename it to `v0-v2`, and after three columns, again I want to have `v0-v2`. Since, it seems that we can not have the repetitive names, I change the second group to `v10-v12`, and third group to `v20-v22` and etc.

``````df = pd.DataFrame()
df['id'] = 
df['v0'] = 
df['v1'] = 
df['v2'] = 
df['v3'] = 
df['v4'] = 
df['v5'] = 
df['v6'] = 
df['v7'] = 
df['v8'] = 
df['v9'] = 
df['v10'] = 
df['v11'] = 
df['v12'] = 
df['v13'] = 
df['v14'] = 
df
``````

And here is the output I want. Thank you in advance

``````   id   v00 v01 v02 v10 v11 v12 v20 v21 v22 v30 v31 v32 v40 v41 v42
0   1   2   1    2   1   2   1   2   1   2   1   2   1   2   1   2
``````

## Solution – 1

You can actually have duplicated names:

``````import numpy as np
df.columns = np.hstack(['id', np.tile(['v0', 'v1', 'v2'], (len(df.columns)-1)//3)])

print(df)
``````

Output:

``````   id  v0  v1  v2  v0  v1  v2  v0  v1  v2  v0  v1  v2  v0  v1  v2
0   1   2   1   2   1   2   1   2   1   2   1   2   1   2   1   2
``````

``````a = np.arange(df.shape-1)
df.columns = np.hstack(['id', new])

print(df)
``````

Output:

``````   id  v0  v1  v2  v10  v11  v12  v20  v21  v22  v30  v31  v32  v40  v41  v42
0   1   2   1   2    1    2    1    2    1    2    1    2    1    2    1    2
``````

#### alternative with `str.replace`:

``````df.columns = df.columns.str.replace('(d+)',
lambda m: str((x:=int(m.group()))%3+(x//3*10)),
regex=True)
``````

## Solution – 2

You can use:

``````df.rename(columns={f"v{i}": f"v{int(i/3)}"+f"{int(i%3)}" for i in range(len(df.columns))}, inplace=True)

``````
Rate this post
We use cookies in order to give you the best possible experience on our website. By continuing to use this site, you agree to our use of cookies.