2 count
aggfunc
, ( ), pd.crosstab
:
>> new_df = pd.crosstab(df.ID, df.Class)
>> new_df
Class A B D Other
ID
1 1 1 0 0
2 0 0 1 0
3 0 1 0 0
4 0 0 0 1
5 0 0 0 1
6 0 0 0 1
7 1 0 0 0
8 0 0 0 1
9 1 0 0 0
:
>> df.groupby('ID').Class.last()
ID
1 B
2 D
3 B
4 Other
5 Other
6 Other
7 A
8 Other
9 A
Then you can combine them with concatenation:
>> new_df = pd.concat([new_df, df.groupby('ID').Class.last()], 1)
A B D Other Class
ID
1 1 1 0 0 B
2 0 0 1 0 D
3 0 1 0 0 B
4 0 0 0 1 Other
5 0 0 0 1 Other
6 0 0 0 1 Other
7 1 0 0 0 A
8 0 0 0 1 Other
9 1 0 0 0 A
And to get the result exactly the way you wanted it:
>> new_df = new_df.rename(columns={'Class':'LastClass'})
A B D Other LastClass
ID
1 1 1 0 0 B
2 0 0 1 0 D
3 0 1 0 0 B
4 0 0 0 1 Other
5 0 0 0 1 Other
6 0 0 0 1 Other
7 1 0 0 0 A
8 0 0 0 1 Other
9 1 0 0 0 A
Putting it all together as an oneliner:
>> new_df = pd.concat([pd.crosstab(df.ID, df.Class),df.groupby('ID').Class.last()],1).rename(columns={'Class':'LastClass'})
>> new_df
A B D Other LastClass
ID
1 1 1 0 0 B
2 0 0 1 0 D
3 0 1 0 0 B
4 0 0 0 1 Other
5 0 0 0 1 Other
6 0 0 0 1 Other
7 1 0 0 0 A
8 0 0 0 1 Other
9 1 0 0 0 A
source
share