Issue
The table I have
{'name': {0: 'Result 1',
1: 'Result 2',
2: 'Result 1',
3: 'Result 2'},
'value': {0: '255.00', 1: '260.00', 2: '255.00', 3: '260.00'},
'Result 3': {0: 325, 1: 325, 2: 285, 3: 285},
'batchnumber': {0: '59530', 1: '59530', 2: '59534', 3: '59534'}}
The transpose I want to do is single column transpose
index | name | value | batchnumber |
---|---|---|---|
0 | Result 1 | 255.00 | 59534 |
1 | Result 2 | 260.00 | 59534 |
2 | Result 3 | 285.00 | 59534 |
3 | Result 1 | 255.00 | 59530 |
4 | Result 2 | 260.00 | 59530 |
5 | Result 3 | 325.00 | 59530 |
I tried pivot
df.pivot(index='Result 3', columns='name', values='batchnumber')
df = df.transpose()
Solution
You can pivot_wider then melt. In this case, you get the same results but rather unmatched rows
df.pivot(['batchnumber', 'Result 3'],'name', 'value').reset_index().melt('batchnumber')
batchnumber name value
0 59530 Result 3 325.0
1 59534 Result 3 285.0
2 59530 Result 1 255.0
3 59534 Result 1 255.0
4 59530 Result 2 260.0
5 59534 Result 2 260.0
You could sort before you melt:
(df.pivot(['batchnumber', 'Result 3'],'name', 'value')
.reset_index()
.sort_index(axis=1)
.melt('batchnumber'))
batchnumber name value
0 59530 Result 1 255.0
1 59534 Result 1 255.0
2 59530 Result 2 260.0
3 59534 Result 2 260.0
4 59530 Result 3 325.0
5 59534 Result 3 285.0
Answered By - onyambu
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.