I'm familiar with the standard Pandas pivot functionality, but what I'm looking to do is a little different - and I'm not married to using pivot
.
Setup code:
import pandas as pd
import io
csvA = io.StringIO(u'''
month chicken_a chicken_b chicken_c turkey_a turkey_b turkey_c
1 10 20 30 1 2 3
2 11 22 33 101 202 303
''')
dfA = pd.read_csv(csvA, sep = '\t')
Which yields:
month chicken_a chicken_b chicken_c turkey_a turkey_b turkey_c
0 1 10 20 30 1 2 3
1 2 11 22 33 101 202 303
I want to partially pivot the '_a', '_b', and '_c'
to the month, but keep the 'chicken' and 'turkey' headers. The final result would look like this:
month chicken turkey
0 1a 10 1
1 1b 20 2
2 1c 30 3
3 2a 11 101
4 2b 22 202
5 2c 33 303
The '_a', '_b', '_c'
part will always be exactly that, and will be known ahead of time.
I could hack this via for
loops, but I'm wondering if there is a more pandanic way.