Home > Enterprise >  Change alternate column names in for loop
Change alternate column names in for loop

Time:08-21

I am new to Pandas and would like to learn how to change alternate column names -

I have a dataframe which looks like this - enter image description here

I would like to change the column names to be like this - enter image description here

Any suggestion which apply a for loop would be helpful as I have 57 column names to change in the desired pattern.

CodePudding user response:

A little verbose, but it works:

import pandas as pd
df = pd.DataFrame(columns = ['Time', 'Jolt1', 'Jolt2', 'Time', 'Jolt1', 'Jolt2', 'Time', 'Jolt1', 'Jolt2'])
c = 1
length = int(len(df.columns)/3)
my_list = []
for i in range(length):
    my_list.append(['Time', 'Jolt' str(c), 'Jolt' str(c   1)])
    c = c   1
df.columns = sum(my_list, [])
  • Related