Python操作,递归多准则操作

2024-04-25 11:31:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我得到了一个数据集,其中包含三个不同的变量(year、ticker和attributes)及其相应的值(参见下面的示例)。你知道吗

我要应用多个操作/计算,方法是选择某些attributes,并确保它们应用于各自的yearticker(例如:2015年AAC的WORKINGCAPITAL除以2015年AAC的ASSETSC),并且需要存储这些计算中每一个的数据。顺便说一下,不要介意日期格式,我已经处理好了。你知道吗

    date        value        ticker attributes
0   2013-12-31  0.000000e+00    AAC ACCOCI
1   2014-12-31  0.000000e+00    AAC ACCOCI
2   2015-12-31  0.000000e+00    AAC ACCOCI
3   2013-12-31  2.952900e+07    AAC ASSETSC
4   2014-12-31  7.992200e+07    AAC ASSETSC
5   2015-12-31  8.652400e+07    AAC ASSETSC
... ... ... ... ...
839968  2009-12-31  1.643200e+07    Z   WORKINGCAPITAL
839969  2010-12-31  1.194100e+07    Z   WORKINGCAPITAL
839970  2011-12-31  7.171300e+07    Z   WORKINGCAPITAL
839971  2012-12-31  1.846610e+08    Z   WORKINGCAPITAL
839972  2013-12-31  2.829030e+08    Z   WORKINGCAPITAL
839973  2014-12-31  3.521410e+08    Z   WORKINGCAPITAL
839974  2015-12-31  4.936720e+08    Z   WORKINGCAPITAL

我想出了两个选择:

  1. 根据属性透视表并按列索引执行操作(例如:results['WrkCapPct'] = df['WORKINGCAPITAL']/df['ASSETSC'])。你知道吗
  2. 使用递归操作并确保它匹配yearticker。你知道吗

Alternative 1 is the simplest for me (at least if I were in R), and I've managed to produce a pivot_table which mostly does the trick.

pd.pivot_table(df, values='value', index=['ticker', 'year'],columns='attributes')

But from that point on, I must admit I'm stuck for the calculations and data storage in a new data frame column.

我已经设法在R中做到了这一点,但是我想用Python来操作,因为我只有一次生命,没有服务器场。你知道吗

For alternative 2, I have tried a few things, but I have no clue how to do this other than brute force it. I would resort to it only if it is most computationally efficient, which I highly doubt.

谢谢你的帮助!你知道吗


Tags: theto数据dfforisvalueit