给定具有一行这样的示例数据框:
brand_1 gen_1 both_1 brand_2 gen_2 both_2 brand_3 gen_3 both_3
6133 5636 5446 0 3239 0 6032 5870 5484
我试图对数据进行以下重塑时,Transpose或Transform无法正常工作:
Type Brand Gen Both
1 6133 5636 5446
2 0 3239 0
3 6032 5870 5484
我已经在列大块上尝试了Transform和Transpose以及iloc,但是没有结果可以这种方式重塑数据。因此,我的问题在这里。
让我们尝试 wide_to_long
out = pd.wide_to_long(df.reset_index(),['brand','gen','both'],i=['index'],j='Type',sep='_').reset_index()
Out[135]:
index Type both_susp_2 brand gen both
0 0 1 0 6133 5636 5446.0
1 0 2 0 0 3239 NaN
2 0 3 0 6032 5870 5484.0
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句