Home > database >  Pivoting dataframe but ignoring index
Pivoting dataframe but ignoring index

Time:09-28

Suppose I have the following dataset. Each week is guaranteed to only have two observations:

data = pd.DataFrame(
    {
        "week": ["2022-09-26", "2022-09-26", "2022-09-19", "2022-09-19", "2022-09-12", "2022-09-12"],
        "name": ["a", "b", "c", "d", "e", "f"],
        "counts": [10, 20, 30, 40, 50, 60]
    }
)

What I wish to do is create a multi-level column, where the first level is week, and the second level is name and counts. Each row would have name and counts displayed for all 3 weeks. And therefore, the output would be 2 rows deep.

Expected output (I'll draw best as I can):

2022-09-12 | 2022-09-19 | 2022-09-26
name | counts | name | counts | name | counts
"e" | 50 | "c" | 30 | "a" | 10
"f" | 60 | "d" | 40 | "b" | 20

Unfortunately the following was not what I was after: enter image description here

CodePudding user response:

Updating the answer based on expected output. Really hoping it helps now :)

    pd.concat(
        [
            df.reset_index().pivot(
                columns=['week'],
                values=['name','counts']
            ).reorder_levels([1,0], axis='columns') 
            for g, df in data.groupby('week')
        ],
        axis=1
    )

enter image description here

Here you go. Hope this helps

df.reset_index().pivot(index='index',columns=['week'],values=['name','counts']).reorder_levels([1,0],axis='columns')


week    2022-09-12  2022-09-19  2022-09-26  2022-09-12  2022-09-19  2022-09-26
name    name    name    counts  counts  counts
index                       
0   NaN NaN a   NaN NaN 10
1   NaN NaN b   NaN NaN 20
2   NaN c   NaN NaN 30  NaN
3   NaN d   NaN NaN 40  NaN
4   e   NaN NaN 50  NaN NaN
5   f   NaN NaN 60  NaN NaN
  • Related