pandas pct_change()反向

时间:2017-12-05 06:30:40

标签: python python-2.7 pandas percentage

假设我们有一个数据帧,我们计算行之间的百分比变化

//Get the sorted order of the test case which is expected order
def newList = testSuite.testCaseList.name.sort()
log.info "Expected order of test cases: ${newList}"

//Get the current index of the test case
def getTestCaseIndex = { name -> testSuite.getIndexOfTestCase(testSuite.getTestCaseByName(name))}

//Closure definition and this is being called recursively to make the desired order
def rearrange
rearrange = {
    def testCaseNames = testSuite.testCaseList.name
    if (testCaseNames != newList) {
        log.info testCaseNames
        newList.eachWithIndex { tc, index ->
            def existingIndex = getTestCaseIndex(tc)
            if (index != existingIndex) {
                testSuite.moveTestCase(index, existingIndex-index)
                rearrange()
            }
        }
    } else {
        log.info 'All cases sorted'
    }
}

//Call the closure
rearrange()

这样它从第一行开始。 我想从最后一行开始计算Setup Script

一种方法

y_axis = [1,2,3,4,5,6,7,8,9]
x_axis = [100,105,115,95,90,88,110,100,0]

DF = pd.DataFrame({'Y':y_axis, 'X':x_axis})

DF = DF[['Y','X']]
DF['PCT'] = DF['X'].pct_change()

    Y   X   PCT
0   1   100 NaN  
1   2   105 0.050000
2   3   115 0.095238
3   4   95  -0.173913
4   5   90  -0.052632
5   6   88  -0.022222
6   7   110 0.250000
7   8   100 -0.090909
8   9   0   -1.000000

但这是一个非常丑陋且效率低下的解决方案。 我想知道是否有更优雅的解决方案?

2 个答案:

答案 0 :(得分:4)

DF.assign(_PCT_=DF.X.pct_change(-1))

   Y    X       PCT     _PCT_
0  1  100       NaN -0.047619
1  2  105  0.050000 -0.086957
2  3  115  0.095238  0.210526
3  4   95 -0.173913  0.055556
4  5   90 -0.052632  0.022727
5  6   88 -0.022222 -0.200000
6  7  110  0.250000  0.100000
7  8  100 -0.090909       inf
8  9    0 -1.000000       NaN
  

Series.pct_change(periods = 1,fill_method ='pad',limit = None,freq = None,** kwargs)

     

期间:int,默认值1为了形成百分比变化而转移的期间

     

https://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.pct_change.html

答案 1 :(得分:2)

我删除了我的其他答案,因为@su79eu7k 's更好。

您可以使用底层数组将时间缩短一半。但你也必须压制警告。

a = DF.X.values
DF.assign(_PCT_=np.append((a[:-1] - a[1:]) / a[1:], np.nan))

   Y    X       PCT     _PCT_
0  1  100       NaN -0.047619
1  2  105  0.050000 -0.086957
2  3  115  0.095238  0.210526
3  4   95 -0.173913  0.055556
4  5   90 -0.052632  0.022727
5  6   88 -0.022222 -0.200000
6  7  110  0.250000  0.100000
7  8  100 -0.090909       inf
8  9    0 -1.000000       NaN