Reputation: 1607
I am trying to messing with pandas stack and unstack. I was wondering if it is possible to reshape my data in this way.
This is my sample data which I am practicing.
ID,Value1,Value2
1,3,12
1,4,13
1,5,14
1,6,15
1,7,16
2,8,17
2,9,18
2,10,19
2,11,20
And I want to reshape in this way.
ID
1 Index(Extra Column) Value1, value2
1 3 12
2 4 13
3 5 14
4 6 15
5 7 16
2
1 8 17
2 9 18
3 10 19
4 11 20
I tried this
df1 = pd.DataFrame(df[['Value1', 'Value2']], index= df['ID']).stack()
or
df1 = df.set_index(['ID']).stack()
this changes Value1 and Value2 from column to rows which I dont want.
Any Ideas ?
Upvotes: 3
Views: 445
Reputation: 294488
defaultdict
and count
from itertools import count
from collections import defaultdict
d = defaultdict(count)
df.set_index(['ID', np.array([next(d[x]) for x in df.ID])])
Value1 Value2
ID
1 0 3 12
1 4 13
2 5 14
3 6 15
4 7 16
2 0 8 17
1 9 18
2 10 19
3 11 20
Upvotes: 2
Reputation: 323326
One way from apply
df.groupby('ID')[['Value1','Value2']].apply(lambda x : x.reset_index(drop=True))
Out[662]:
Value1 Value2
ID
1 0 3 12
1 4 13
2 5 14
3 6 15
4 7 16
2 0 8 17
1 9 18
2 10 19
3 11 20
Upvotes: 3
Reputation: 402814
I propose set_index
+ cumcount
here:
df.set_index(['ID', df.groupby('ID').cumcount() + 1])
Value1 Value2
ID
1 1 3 12
2 4 13
3 5 14
4 6 15
5 7 16
2 1 8 17
2 9 18
3 10 19
4 11 20
Another option is using concat
:
pd.concat({k : g.reset_index(drop=True) for k, g in df.drop('ID', 1).groupby(df.ID)})
Value1 Value2
1 0 3 12
1 4 13
2 5 14
3 6 15
4 7 16
2 0 8 17
1 9 18
2 10 19
3 11 20
Upvotes: 4