-1

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    
Prasad Patil
  • 45
  • 1
  • 8
  • can you post the output of `df.head().to_dict()`? – Anurag Dabas Jun 15 '21 at 12:28
  • df.head().to_dict() = {('A', ''): {0: 'e1', 1: 'e1', 2: 'e1', 3: 'e2', 4: 'e2'}, ('B', ''): {0: 'f1', 1: 'f2', 2: 'f3', 3: 'f1', 4: 'f2'}, ('volumeshare', Timestamp('2020-10-01 00:00:00')): {0: 12, 1: 23, 2: 0, 3: 76, 4: 89}, ('volshare', Timestamp('2020-11-01 00:00:00')): {65, 1: 20, 2: 91, 3: 3, 4: 33}} – Prasad Patil Jun 15 '21 at 12:36

1 Answers1

1

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
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252