shantanuo
shantanuo

Reputation: 32336

Finding the percent change of values in a Series

I have a DataFrame with 2 columns. I need to know at what point the number of questions has increased.

In [19]: status
Out[19]:
   seconds  questions
0   751479    9005591
1   751539    9207129
2   751599    9208994
3   751659    9210429
4   751719    9211944
5   751779    9213287
6   751839    9214916
7   751899    9215924
8   751959    9216676
9   752019    9217533

I need the change in percent of 'questions' column and then sort on it. This does not work:

status.pct_change('questions').sort('questions').head()

Any suggestions?

Upvotes: 1

Views: 4853

Answers (1)

Alex Riley
Alex Riley

Reputation: 176938

Try this way instead:

>>> status['change'] = status.questions.pct_change()
>>> status.sort_values('change', ascending=False)

   questions  seconds    change
0    9005591   751479       NaN
1    9207129   751539  0.022379
2    9208994   751599  0.000203
6    9214916   751839  0.000177
4    9211944   751719  0.000164
3    9210429   751659  0.000156
5    9213287   751779  0.000146
7    9215924   751899  0.000109
9    9217533   752019  0.000093
8    9216676   751959  0.000082

pct_change can be performed on Series as well as DataFrames and accepts an integer argument for the number of periods you want to calculate the change over (the default is 1).

I've also assumed that you want to sort on the 'change' column with the greatest percentage changes showing first...

Upvotes: 3

Related Questions