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 -
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
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