gpt4 book ai didi

python - 如何正确堆叠或取消堆叠 pandas 数据框?

转载 作者:行者123 更新时间:2023-12-01 00:01:35 25 4
gpt4 key购买 nike

我一直在尝试堆叠或取消堆叠以下数据框:

       Eelde  Southend-On-Sea     Ulceby        date
24.000000 NaN NaN 2020-02-16
1 22.673913 NaN NaN 2020-02-17
0 NaN NaN 24.312500 2020-02-17
0 NaN 19.130435 NaN 2020-02-17
0 14.000000 NaN NaN 2020-02-17
1 19.239130 NaN NaN 2020-02-18
0 NaN NaN 19.687500 2020-02-18
0 NaN 17.760870 NaN 2020-02-18
0 20.500000 NaN NaN 2020-02-18
1 12.978261 NaN NaN 2020-02-19
0 NaN NaN 19.041667 2020-02-19
0 NaN 14.200000 NaN 2020-02-19

我们不要关注双索引,而是关注组合行。

所需的输出应如下所示:

   index     Eelde  Southend-On-Sea      Ulceby     date
0 24.000000 NaN NaN 2020-02-16
0 0 14.00000 19.130435 24.312500 2020-02-17
1 1 19.23913 17.76087 19.6875 2020-02-18
2 0 12.978261 14.200000 19.041667 2020-02-19

最佳答案

如果想要每组每列的第一个非缺失值(如果存在),请使用 GroupBy.first :

df1 = df.groupby('date', as_index=False).first()

关于python - 如何正确堆叠或取消堆叠 pandas 数据框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60316772/

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