本文介绍了Groupby ID以计算比率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

客观

我有这个df,下面有一些比例.我想按每个id和datadate计算这些比率,我相信groupby函数是可行的方法,但是我不确定.任何帮助都将是超级!

I have this df and take some ratios below. I want to calculate these ratios by each id and datadate and I believe the groupby function is the way to go, however I am not exactly sure. Any help would be super!

df

     id   datadate    dltt   ceq  ...        pstk     icapt  dlc      sale
1  001004 1975-02-28  3.0  193.0  ...      1.012793     1    0.20    7.367237
2  001004 1975-05-31  4.0  197.0  ...      1.249831     1    0.21    8.982741
3  001004 1975-08-31  5.0  174.0  ...      1.142086     2    0.24    8.115609
4  001004 1975-11-30  8.0  974.0  ...      1.400673     3    0.26    9.944990
5  001005 1975-02-28  3.0  191.0  ...      1.012793     4    0.25    7.367237
6  001005 1975-05-31  3.0  971.0  ...      1.249831     5    0.26    8.982741
7  001005 1975-08-31  2.0  975.0  ...      1.142086     6    0.27    8.115609
8  001005 1975-11-30  1.0  197.0  ...      1.400673     3    0.27    9.944990
9  001006 1975-02-28  3.0  974.0  ...      1.012793     2    0.28    7.367237
10 001006 1975-05-31  4.0  74.0   ...      1.249831     1    0.21    8.982741
11 001006 1975-08-31  5.0  75.0   ...      1.142086     3    0.23    8.115609
12 001006 1975-11-30  5.0  197.0  ...      1.400673     4    0.24    9.944990

比率示例

df['capital_ratioa'] = df['dltt']/(df['dltt']+df['ceq']+df['pstk'])
df['equity_invcapa'] = df['ceq']/df['icapt']
df['debt_invcapa'] = df['dltt']/df['icapt']
df['sale_invcapa']=df['sale']/df['icapt']
df['totdebt_invcapa']=(df['dltt']+df['dlc'])/df['icapt']

推荐答案

这是您要寻找的吗?

df = df.groupby(by=['id'], as_index=False).sum()
df['capital_ratioa'] = df['dltt']/(df['dltt']+df['ceq']+df['pstk'])
df['equity_invcapa'] = df['ceq']/df['icapt']
df['debt_invcapa'] = df['dltt']/df['icapt']
df['sale_invcapa']=df['sale']/df['icapt']
df['totdebt_invcapa']=(df['dltt']+df['dlc'])/df['icapt']
print(df)

输出:

     id  dltt     ceq      pstk  icapt   dlc       sale  capital_ratioa  equity_invcapa  debt_invcapa  sale_invcapa  totdebt_invcapa
0  1004  20.0  1538.0  4.805383      7  0.91  34.410577        0.012797      219.714286      2.857143      4.915797         2.987143
1  1005   9.0  2334.0  4.805383     18  1.05  34.410577        0.003833      129.666667      0.500000      1.911699         0.558333
2  1006  17.0  1320.0  4.805383     10  0.96  34.410577        0.012669      132.000000      1.700000      3.441058         1.796000

这篇关于Groupby ID以计算比率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-04 23:54