我有这个作为数据框:
custid day freq
346782 1 0
346782 0 1
346782 1 2
346783 0 0
346783 0 1
346783 0 2
但是出于机器学习的目的,我想将其半转换为:
346782 1 0 0 1 1 2
346783 0 0 0 1 0 2
您知道,因此custID仅在其前面的所有行中带有其所有相关功能一次。
我已经尝试过各种方法,例如:
df1 = pd.melt(newdf, id_vars=['0']).drop('variable', axis=1).sort_values(0)
我怎样才能完成这一转变?
我在stack
这里使用,您也可以尝试melt
s=df.set_index('custid').stack()
s.index=pd.MultiIndex.from_arrays([s.index.get_level_values(level=0),s.groupby(level=0).cumcount()])
s.unstack()
Out[843]:
0 1 2 3 4 5
custid
346782 1 0 0 1 1 2
346783 0 0 0 1 0 2
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句