Search code examples
pythonpandasdataframemulti-indexdata-wrangling

How to rename timestamp column names to string/object in multiindex dataframe using python


DataFrame :df

None  | A  B   volumeshare    volumeshare
X     |         2020-10-1      2020-11-1
---------------------------------------
0     | e1 f1      12            65    
1     | e1 f2      23            20     
2     | e1 f3      0             91    
3     | e2 f1      76            3     
4     | e2 f2      89            33    

I wish to achieve the below things -

  1. Only one top row with X, A, B, 2020-10-1, 2020-11-1 as the column names in that order.
  2. column 3 and 4 have timestamp names (2020-10-1 & 2020-11-1), need to replace it into string as Oct-20 and Nov-20 respectively.

Expected output

X     | A   B      Oct-20       Nov-20
---------------------------------------
0     | e1 f1      12            65    
1     | e1 f2      23            20     
2     | e1 f3      0             91    
3     | e2 f1      76            3     
4     | e2 f2      89            33    

Solution

  • Use custom lambda function in list comprehension:

    def f(a, b):
        d = pd.to_datetime(b, errors='coerce')
        return d.strftime('%b-%y') if pd.notna(d) else a
       
    df.columns = [f(a, b) for a, b in df.columns]
    print (df)
        A   B  Oct-20  Nov-20
    0  e1  f1      12      65
    1  e1  f2      23      20
    2  e1  f3       0      91
    3  e2  f1      76       3
    4  e2  f2      89      33