基于分类列和另一列中的值创建列

问题描述 投票:2回答:1

我的数据看起来像这样

Weight  Sex
91.0    male
91.0    male
131.0   female
128.0   female
132.0   female
150.0   male

我想要类似的东西>

male   female
91.0     0
91.0     0
0        131.0
0        128.0
0        132.0
150.0     0

不使用pd.wide_to_long()函数如何实现此目的。

我的数据看起来像这样体重性别91.0男性91.0男性131.0女性128.0女性132.0女性150.0男我想要类似男性女性91.0 0 91.0 0 0 131.0 0 ...

python pandas
1个回答
4
投票

不使用DataFrame.pivot参数使用DataFrame.pivot,然后用index替换缺少的值:

DataFrame.fillna
© www.soinside.com 2019 - 2024. All rights reserved.