gpt4 book ai didi

Python/Pandas - 计算比率

转载 作者:太空宇宙 更新时间:2023-11-03 14:51:48 26 4
gpt4 key购买 nike

我有这个数据框:

bal:

year id unit period Revenues Ativo Não-Circulante \
business_id
9564 2012 302 dsada anual 5964168.52 10976013.70
9564 2011 303 dsada anual 5774707.15 10867868.13
2361 2013 304 dsada anual 3652575.31 6608468.52
2361 2012 305 dsada anual 321076.15 6027066.03
2361 2011 306 dsada anual 3858137.49 9733126.02
2369 2012 307 dsada anual 351373.66 9402830.89
8104 2012 308 dsada anual 3503226.02 6267307.01
...

我想创建一个名为“Growth”的列。这将是:

(今年收入/去年收入)- 1

数据框应如下所示:

             year   id   unit period          Revenues               Growth  \
business_id
9564 2012 302 dsada anual 5964168.52 0.0328
9564 2011 303 dsada anual 5774707.15 NaN
2361 2013 304 dsada anual 3652575.31 10.37
2361 2012 305 dsada anual 321076.15 -0.91
2361 2011 306 dsada anual 3858137.49 NaN
2369 2012 307 dsada anual 351373.66 NaN
8104 2012 308 dsada anual 3503226.02 NaN
...

我怎样才能做到这一点?

最佳答案

我假设您的数据框名为df。首先休息索引,使 business_id 成为一列,然后按 year 对结果进行排序。现在,根据 business_id 对数据帧进行分组,并转换结果以获得收入的百分比变化。最后利用索引得到原始顺序。

df2 = df.reset_index().sort_values(['year'])
df2 = (
df2
.assign(Growth=df2.groupby(['business_id'])['Revenues'].transform(
lambda group: group.pct_change()))
.sort_index()
)
>>> df2
business_id year id unit period Revenues Ativo Não-Circulante Growth
0 9564 2012 302 dsada anual 5964168.52 10976013.70 0.032809
1 9564 2011 303 dsada anual 5774707.15 10867868.13 NaN
2 2361 2013 304 dsada anual 3652575.31 6608468.52 10.376041
3 2361 2012 305 dsada anual 321076.15 6027066.03 -0.916779
4 2361 2011 306 dsada anual 3858137.49 9733126.02 NaN
5 2369 2012 307 dsada anual 351373.66 9402830.89 NaN
6 8104 2012 308 dsada anual 3503226.02 6267307.01 NaN

我认为您的预期输出有错误:

5964168.52 / 5774707.15 - 1 = 0.0328  # vs. 0.16 shown.

关于Python/Pandas - 计算比率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45853159/

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