Reputation: 341
For the below, is there any way to format months till December as 'DEC-2017', based on the value in index 0 and months after December as 'JAN-2018'? I'm using Python 2.7.
import pandas as pd
df = pd.DataFrame([['2017-18','','','','','','','','','','',''], ['APR', 'MAY', 'JUN', 'JULY', 'AUG', 'SEP', 'OCT', 'NOV', 'DEC', 'JAN', 'FEB', 'MAR']])
output should look like:
df1 = pd.DataFrame([['2017-18','','','','','','','','','','',''], ['APR-2017', 'MAY-2017', 'JUN-2017', 'JULY-2017', 'AUG-2017', 'SEP-2017', 'OCT-2017', 'NOV-2017', 'DEC-2017', 'JAN-2018', 'FEB-2018', 'MAR-2018']])
sorry I'm new so don't know how to paste a dataframe here and thus using codes.
Upvotes: 1
Views: 37
Reputation: 403130
cumsum
+ iloc
df.iloc[1] += '-' + (df.iloc[1].eq('JAN').cumsum() + 2017).astype(str)
df
0 1 2 3 4 5 6 \
0 2017-18
1 APR-2017 MAY-2017 JUN-2017 JULY-2017 AUG-2017 SEP-2017 OCT-2017
7 8 9 10 11
0
1 NOV-2017 DEC-2017 JAN-2018 FEB-2018 MAR-2018
Upvotes: 1