Reputation: 5414
Here is my data frame :
df = pd.DataFrame({'C1_val': {1: 1.1,2: 2.6,3: 1.1,4: 2.6,5: 1.8,6: 3.6,7: 1.8,8: 3.6},
'ID': {1: 'x1',2: 'x1',3: 'x1',4: 'x1',5: 'x2',6: 'x2',7: 'x2',8: 'x2'},
'T1_val': {1: 1.1,2: 1.1,3: 2.2,4: 2.2,5: 1.8,6: 1.8,7: 7.9,8: 7.9},
'name1': {1: 'T1',2: 'T1',3: 'T2',4: 'T2',5: 'T1',6: 'T1',7: 'T2',8: 'T2'},
'name2': {1: 'C1',2: 'C2',3: 'C1',4: 'C2',5: 'C1',6: 'C2',7: 'C1',8: 'C2'}})
it looks like this:
C1_val ID T1_val name1 name2
1 1.1 x1 1.1 T1 C1
2 2.6 x1 1.1 T1 C2
3 1.1 x1 2.2 T2 C1
4 2.6 x1 2.2 T2 C2
5 1.8 x2 1.8 T1 C1
6 3.6 x2 1.8 T1 C2
7 1.8 x2 7.9 T2 C1
8 3.6 x2 7.9 T2 C2
I want to reshape the data to be like this
final_res = pd.DataFrame({'C1': {1: 1.1, 2: 1.8},
'C2': {1: 2.6, 2: 3.6},
'ID': {1: 'x1', 2: 'x2'},
'T1': {1: 1.1, 2: 1.8},
'T2': {1: 2.2, 2: 7.9}})
the final result would be:
ID T1 T2 C1 C2
1 x1 1.1 2.3 1.1 2.6
2 x2 1.8 7.9 1.8 3.6
I've tried many many things using both pivot
function or using both set_index
with unstack
that was my best attempt to solve the problem
df.set_index(['ID', 'name1', 'name2']).unstack('name1').unstack('name2')
this can be solved easily in R
using tidyr
package by simply doing this
df %>%
spread(name1, T1_val) %>%
spread(name2, C1_val)
Upvotes: 0
Views: 66
Reputation: 77951
use .pivot_table
followed by concat
:
>>> a = df.pivot_table(index='ID', columns='name1', values='T1_val')
>>> b = df.pivot_table(index='ID', columns='name2', values='C1_val')
>>> pd.concat((a, b), axis=1).reset_index()
ID T1 T2 C1 C2
0 x1 1.1 2.2 1.1 2.6
1 x2 1.8 7.9 1.8 3.6
Upvotes: 2