A B X Y
0 a aa 1.0 5.0
1 b bb 6.0 2.0
2 c cc 3.0 7.0
3 d dd 8.0 4.0
Have you tried df.pivot() or pd.pivot()? The values in column C will become column headers. After that, flatten the multi-index columns, and rename them.
import pandas as pd
#df = df.pivot(['A', 'B'], columns='C').reset_index() #this also works
df = pd.pivot(data=df, index=['A', 'B'], columns='C').reset_index()
df.columns = ['A', 'B', 'X', 'Y']
print(df)
Output
A B X Y
0 a aa 1 5
1 b bb 6 2
2 c cc 3 7
3 d dd 8 4
Sometimes, there might be repeated records with the same index, then you'd have to use pd.pivot_table() instead. The param aggfunc=np.mean will take the mean of these repeated records, and become type float as you can see from the output.
发布评论
评论(2)
您是否尝试过
df.pivot()
或pd.pivot()
?列C
中的值将成为列标题。之后,将多指数列缩放并重命名它们。有时输出
可能会有具有相同索引的重复记录,然后您必须改用
pd.pivot_table()
。 paramaggfunc = np.mean
将占据这些重复记录的平均值,并成为从输出中看到的float
。输出
Have you tried
df.pivot()
orpd.pivot()
? The values in columnC
will become column headers. After that, flatten the multi-index columns, and rename them.Output
Sometimes, there might be repeated records with the same index, then you'd have to use
pd.pivot_table()
instead. The paramaggfunc=np.mean
will take the mean of these repeated records, and become typefloat
as you can see from the output.Output
您可以尝试
You can try