gpt4 book ai didi

python - 将日期框架重新格式化为新的输出格式

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

我有数据帧(df)中数据透视表的输出,如下所示:

  Year Month             sum
2005 10 -1.596817e+05
11 -2.521054e+05
12 5.981900e+05
2006 1 8.686413e+05
2 1.673673e+06
3 1.218341e+06
4 4.131970e+05
5 1.090499e+05
6 1.495985e+06
7 1.736795e+06
8 1.155071e+05
...
9 7.847369e+05
10 -5.564139e+04
11 -7.435682e+05
12 1.073361e+05
2017 1 3.427652e+05
2 3.574432e+05
3 5.026018e+04

有没有办法重新格式化数据帧,以便控制台的输出如下所示:

Month     1  2  3  4  5  6  7  8  9  10  11  12
Year
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017

所有值也将填充到新表中。

最佳答案

使用unstack:

In [18]: df['sum'].unstack('Month')
Out[18]:
Month 1 2 3 4 5 6 7 8 9 10 11 12
Year
2005.0 NaN NaN NaN NaN NaN NaN NaN NaN NaN -159681.70 -252105.4 598190.0
2006.0 868641.3 1673673.0 1218341.00 413197.0 109049.9 1495985.0 1736795.0 115507.1 784736.9 -55641.39 -743568.2 107336.1
2017.0 342765.2 357443.2 50260.18 NaN NaN NaN NaN NaN NaN NaN NaN NaN

关于python - 将日期框架重新格式化为新的输出格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47582680/

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