这是一张表,需要按类进行部分取消。
ID Class Type 2017 2018
12A A Net 1 7
12B A Gross 8
12A B Net 3 9
12B B Gross 4 10
13A A Net 5 11
13C B Net 6 5
预期结果:
ID Class Type 2017A 2018A 2017B 2018B
12A A Net 1 7 3 9
12B A Gross NaN 8 4 10
13A A Net 5 11 NaN NaN
13C B Net NaN NaN 6 5
答案 0 :(得分:1)
使用:
df1 = df.set_index(['ID','Type','Class']).unstack().sort_index(level=1, axis=1)
df1.columns = ['{}{}'.format(a,b) for a, b in df1.columns]
df1 = df1.reset_index()
s = df.drop_duplicates('ID').set_index('ID')['Class']
df1.insert(1, 'Class', df1['ID'].map(s))
print (df1)
ID Class Type 2017A 2018A 2017B 2018B
0 12A A Net 1.0 7.0 3.0 9.0
1 12B A Gross NaN 8.0 4.0 10.0
2 13A A Net 5.0 11.0 NaN NaN
3 13C B Net NaN NaN 6.0 5.0
<强>解释强>:
set_index
和unstack
重新整理,按sort_index
MultiIndex
Class
,请使用insert
为map