LearningSlowly
LearningSlowly

Reputation: 9431

Pandas rename column based on column to the left

Consider a dataframe, df:

Metric_a Status.1 Metric_b Status.2, Metric_c, Status.3
10       kg       11       mg        15        ml
12       kg       21       mg        25        ml
130      kg       41       mg        35        ml

I read this in from a csv which uses multiple Status columns. Pandas naturally index's these.

I wish to use the column to the left of each Status column to rename the Status column. From inspection I believe that order is maintained and that the column to the left is always appropriate.

Desired output:

Metric_a Status_Metric_a Metric_b Status_Metric_b Metric_c, Status_Metric_c
    10       kg       11       mg        15        ml
    12       kg       21       mg        25        ml
    130      kg       41       mg        35        ml

Upvotes: 1

Views: 66

Answers (2)

jezrael
jezrael

Reputation: 862511

You can use mask and ffill (it works only with Series, so is necessary convert index to_series):

a = df.columns.to_series()
mask = a.str.contains('Status')
df.columns = a.mask(mask, 'Status_' + a.mask(mask).ffill())
print (df)
   Metric_a Status_Metric_a  Metric_b Status_Metric_b  Metric_c  \
0        10              kg        11              mg        15   
1        12              kg        21              mg        25   
2       130              kg        41              mg        35   

  Status_Metric_c  
0              ml  
1              ml  
2              ml  

Another solution with numpy.where and shift:

a = df.columns.to_series()
df.columns = np.where(a.str.contains('Status'), 'Status_' + a.shift(), a)
print (df)
   Metric_a Status_Metric_a  Metric_b Status_Metric_b  Metric_c  \
0        10              kg        11              mg        15   
1        12              kg        21              mg        25   
2       130              kg        41              mg        35   

  Status_Metric_c  
0              ml  
1              ml  
2              ml  

Upvotes: 1

YGouddi
YGouddi

Reputation: 361

You can use the rename method pandas provide. For example :

df.rename(columns={'Status.1': 'Status_Metric_a'}, inplace=True)

Upvotes: 0

Related Questions