gpt4 book ai didi

python - 按列分组并将另一列的值分散到其他列中

转载 作者:行者123 更新时间:2023-12-03 21:45:46 24 4
gpt4 key购买 nike

我有当前的数据框,我正在尝试按 Name 分组并传播 weight 的值进入列并计算每次出现的次数。谢谢!

df = pd.DataFrame({'Name':['John','Paul','Darren','John','Darren'],
'Weight':['Average','Below Average','Above Average','Average','Above Average']})
期望的输出:
enter image description here

最佳答案

试试 pandas crosstab :

pd.crosstab(df.Name, df.Weight)

Weight Above Average Average Below Average
Name
John 0 2 0
Paul 0 0 1
Darren 2 0 0

关于python - 按列分组并将另一列的值分散到其他列中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64381126/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com