My apologies if this has already been answered elsewhere. I had a difficult time finding an answer that worked for this data, and a harder time coming up with a very specific question for the title.
I have a DataFrame that has Year, Month, and MyValue columns. There is one row for each month and year. It looks something like this:
Month Year MyValue
0 1 1993 -0.070050
1 2 1993 0.994796
2 3 1993 1.402525
3 4 1993 -2.693481
4 5 1993 2.551379
...
11 12 1993 3.123456
12 1 1994 1.003025
...
I would like to restructure it to something that has one row for each year, and has a column for each month that contains the value. Here's an example of what I'd like it to look like:
Year Month 1 Month 2 Month 3 Month 4 Month 5 ... Month 12
0 1993 -0.070050 0.994796 1.402525 -2.693481 2.551379 3.123456
1 1994 1.003025 0.973212 1.313212 -3.554433 1.123123 1.122345
...
I have tried using code like this:
df = df.pivot_table(index=['Year'], columns=['Month']).fillna(0)
However, it groups my values in a weird tuple that makes it more difficult to index and reference things with.
Data columns (total 12 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 (MyValue, 1) 29 non-null float64
1 (MyValue, 2) 29 non-null float64
2 (MyValue, 3) 29 non-null float64
3 (MyValue, 4) 29 non-null float64
4 (MyValue, 5) 29 non-null float64
5 (MyValue, 6) 29 non-null float64
6 (MyValue, 7) 29 non-null float64
7 (MyValue, 8) 29 non-null float64
8 (MyValue, 9) 29 non-null float64
9 (MyValue, 10) 29 non-null float64
10 (MyValue, 11) 29 non-null float64
11 (MyValue, 12) 29 non-null float64
dtypes: float64(12)