Home > Mobile >  Column Header rename loop in Python
Column Header rename loop in Python

Time:08-09

Looking for a solution using python to rename the column headers in the below example DataFrame starting at GStart:GS1. I need to rename the 2 columns adjacent to the already named column and repeat this for the whole data frame.

For example, find the next named, rename the next two columns and repeat. I can complete this using the rename pandas method but is there a way to create a loop as the named column headers will not always be the same.

Example DataFrame:

enter image description here

CodePudding user response:

You can try below code to rename the column names. I assume that there is always a set of 3 columns to rename from 'GStart:GS1' onwards.

#define the original list of column names
df_columns = list(df.columns)

#extract the relevant column names to rename
col_list = df_columns[ df_columns.index('GStart:GS1') : ]

#using this loop with step=3 to look for the starting column name, and append column names with 'A', 'B', 'C' for each set of 3 columns
for i in range(0, len(col_list), 3):
    name = col_list[i]
    col_list[i]   = name 'A'
    col_list[i 1] = name 'B'
    col_list[i 2] = name 'C'

#replace with new column names
df_columns[ df_columns.index('GStart:GS1') : ] = col_list
df.columns = df_columns

Output:

['NaN',
 'NaN',
 'GStart:GS1A',
 'GStart:GS1B',
 'GStart:GS1C',
 'GStart:GS2A',
 'GStart:GS2B',
 'GStart:GS2C',
 'GHIP_Test:HIP1A',
 'GHIP_Test:HIP1B',
 'GHIP_Test:HIP1C',
 'GHIP_Test:HIP2A',
 'GHIP_Test:HIP2B',
 'GHIP_Test:HIP2C',
 'GHIP_Test:HIP3A',
 'GHIP_Test:HIP3B',
 'GHIP_Test:HIP3C']
  • Related