我的数据集如下:
UniqueID CategoryType Value
A Cat1 apple
A Cat2 banana
B Cat1 orange
C Cat2 news
D Cat1 orange
D Cat2 blue
我希望它看起来像:
UniqueID Cat1 Cat2
A apple banana
B orange
C news
D orange blue
我尝试过使用unstack,但无法获得正确的索引集等。
由于
答案 0 :(得分:4)
大部分工作都是用
完成的df.set_index(['UniqueID', 'CategoryType']).Value.unstack(fill_value='')
CategoryType Cat1 Cat2
UniqueID
A apple banana
B orange
C news
D orange blue
我们可以使用
获取剩余的格式df.set_index(['UniqueID', 'CategoryType']).Value.unstack(fill_value='') \
.rename_axis(None, 1).reset_index()
UniqueID Cat1 Cat2
0 A apple banana
1 B orange
2 C news
3 D orange blue
答案 1 :(得分:2)
您可以使用pivot
编辑:从@ piRsquared的回答中获得更多编辑和灵感,
tfsbuild start http://tfs.you.com:8080/tfs/MYPC XYZ Release_Build.v1.0.9 /priority:Normal /requestedfor:TFSSERVICE
答案 2 :(得分:1)
您可以将pivot_table
与fill_value
df.pivot_table(index='UniqueID', columns='CategoryType', values='Value',
aggfunc='sum', fill_value='')
CategoryType Cat1 Cat2
UniqueID
A apple banana
B orange
C news
D orange blue
答案 3 :(得分:0)
pivot
效果很好:
df = df.pivot(index = "UniqueID", columns = "CategoryType", values = "Value")
答案 4 :(得分:0)
带我这么长时间在盒子外思考:)
index = df.UniqueID.unique()
columns = df.CategoryType.unique()
df1= pd.DataFrame(index=index, columns=columns)
df['match']=df.UniqueID.astype(str)+df.CategoryType
A=dict( zip( df.match, df.Value))
df1.apply(lambda x : (x.index+x.name)).applymap(A.get).replace({None:''})
Out[406]:
Cat1 Cat2
A apple banana
B orange
C news
D orange blue