gpt4 book ai didi

python - 使用列名 reshape 长到宽

转载 作者:太空狗 更新时间:2023-10-29 21:14:56 25 4
gpt4 key购买 nike

您好,我在 reshape 我的 df 时遇到了问题。

我有:

Netflix     TV      DVD 
0.1 0.2 0.3
0.12 0.5 0.15
0.4 0.6 0.8
0.5 0.41
0.41
0.2

我想将我的 df 转换为:

Netflix  [0.1, 0.12, 0.4]
TV [0.2, 0.5, 0.6, 0.5, 0.41, 0.2]
DVD [0.3, 0.15, 0.8, 0.41]

不确定 stack()pivot() 如何在这种 df 上工作。任何帮助表示赞赏。

最佳答案

堆栈

堆叠在 reshape 数组时丢弃空值

df.stack().groupby(level=1).agg(list)

DVD [0.3, 0.15, 0.8, 0.41]
Netflix [0.1, 0.12, 0.4]
TV [0.2, 0.5, 0.6, 0.5, 0.41, 0.2]
dtype: object

关于python - 使用列名 reshape 长到宽,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56936921/

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