How do i calculate the percentage increase or decrease in a list of values with python
Question:
I want to calculate the percentage change either increase or decrease in a list.
here is my list
ages = [20.3, 30.5, 20.3, 45.5, 50.6, 29.5, 13.4, 140.9]
here is my code
def percent_change_in_naira(value):
try:
for i in value:
if old_num > new_num:
((((old_num-new_num)/old_num)*100))
elif old_num == new_num:
0
else:
((((new_num-old_num)/old_num)*100))
return value
except ZeroDivisionError:
return 0
How do assign ‘new_num’ to a new number on the list above and ‘old_num’ to a previous number on the list above?
Thanks in advance
Answers:
You can try this: Will get current and next value by index value of list
def percent_change_in_naira(value):
output = []
try:
for index,i in enumerate(range(len(value))):
if i == 0:
output.append(i)
else:
old_num = value[index-1]
new_num = value[index]
output.append(round((((old_num-new_num)/old_num)*100), 3))
except ZeroDivisionError:
output.append(0)
return output
expected output is:
[0, -50.246, 33.443, -124.138, -11.209, 41.7, 54.576, -951.493]
You can use a list comprehension:
i = [20.3, 30.5, 20.3, 45.5, 50.6, 29.5, 13.4, 140.9]
i = [0]+[(i[n+1]-i[n])/i[n]*100 if i[n] else 0 for n in range(len(i)-2)]
print(i)
Output:
[0, 50.2463054187192, -33.44262295081967, 124.13793103448273, 11.208791208791212, -41.69960474308301, -54.576271186440685]
The simplest (and fastest) way calculate running percent changes is with a numpy.array.
import numpy as np
li = np.array([20.3, 30.5, 20.3, 45.5, 50.6, 29.5, 13.4, 140.9])
perc=[0.0]+list(np.round((li[1:]/li[:-1]-1)*100,decimals=1))
print(perc)
Output:
[0.0, 50.2, -33.4, 124.1, 11.2, -41.7, -54.6, 951.5]
Note that if you hit a division by zero, numpy will automatically put ‘inf’ as a result.
In Python (as suggested by @sahasrara62 in the comments)
ages = [20.3, 30.5, 20.3, 45.5, 50.6, 29.5, 13.4, 140.9]
changes = []
for x1, x2 in zip(ages[:-1], ages[1:]):
try:
pct = (x2 - x1) * 100 / x1
except ZeroDivisionError:
pct = None
changes.append(pct)
# [50.2463054187192,
# -33.44262295081967,
# 124.13793103448275,
# 11.208791208791212,
# -41.699604743083,
# -54.576271186440685,
# 951.4925373134328]
Using numpy
import numpy as np
ages = np.array([20.3, 30.5, 20.3, 45.5, 50.6, 29.5,
13.4, 140.9])
diff = ages[1:] - ages[:-1]
changes = diff * 100 / ages[:-1]
# [ 50.24630542 -33.44262295 124.13793103 11.20879121
# -41.69960474 -54.57627119 951.49253731]
Using Pandas
import pandas as pd
ages = pd.Series([20.3, 30.5, 20.3, 45.5, 50.6, 29.5,
13.4, 140.9])
changes = ages.pct_change() * 100
# 0 NaN
# 1 50.246305
# 2 -33.442623
# 3 124.137931
# 4 11.208791
# 5 -41.699605
# 6 -54.576271
# 7 951.492537
# dtype: float64
I want to calculate the percentage change either increase or decrease in a list.
here is my list
ages = [20.3, 30.5, 20.3, 45.5, 50.6, 29.5, 13.4, 140.9]
here is my code
def percent_change_in_naira(value):
try:
for i in value:
if old_num > new_num:
((((old_num-new_num)/old_num)*100))
elif old_num == new_num:
0
else:
((((new_num-old_num)/old_num)*100))
return value
except ZeroDivisionError:
return 0
How do assign ‘new_num’ to a new number on the list above and ‘old_num’ to a previous number on the list above?
Thanks in advance
You can try this: Will get current and next value by index value of list
def percent_change_in_naira(value):
output = []
try:
for index,i in enumerate(range(len(value))):
if i == 0:
output.append(i)
else:
old_num = value[index-1]
new_num = value[index]
output.append(round((((old_num-new_num)/old_num)*100), 3))
except ZeroDivisionError:
output.append(0)
return output
expected output is:
[0, -50.246, 33.443, -124.138, -11.209, 41.7, 54.576, -951.493]
You can use a list comprehension:
i = [20.3, 30.5, 20.3, 45.5, 50.6, 29.5, 13.4, 140.9]
i = [0]+[(i[n+1]-i[n])/i[n]*100 if i[n] else 0 for n in range(len(i)-2)]
print(i)
Output:
[0, 50.2463054187192, -33.44262295081967, 124.13793103448273, 11.208791208791212, -41.69960474308301, -54.576271186440685]
The simplest (and fastest) way calculate running percent changes is with a numpy.array.
import numpy as np
li = np.array([20.3, 30.5, 20.3, 45.5, 50.6, 29.5, 13.4, 140.9])
perc=[0.0]+list(np.round((li[1:]/li[:-1]-1)*100,decimals=1))
print(perc)
Output:
[0.0, 50.2, -33.4, 124.1, 11.2, -41.7, -54.6, 951.5]
Note that if you hit a division by zero, numpy will automatically put ‘inf’ as a result.
In Python (as suggested by @sahasrara62 in the comments)
ages = [20.3, 30.5, 20.3, 45.5, 50.6, 29.5, 13.4, 140.9]
changes = []
for x1, x2 in zip(ages[:-1], ages[1:]):
try:
pct = (x2 - x1) * 100 / x1
except ZeroDivisionError:
pct = None
changes.append(pct)
# [50.2463054187192,
# -33.44262295081967,
# 124.13793103448275,
# 11.208791208791212,
# -41.699604743083,
# -54.576271186440685,
# 951.4925373134328]
Using numpy
import numpy as np
ages = np.array([20.3, 30.5, 20.3, 45.5, 50.6, 29.5,
13.4, 140.9])
diff = ages[1:] - ages[:-1]
changes = diff * 100 / ages[:-1]
# [ 50.24630542 -33.44262295 124.13793103 11.20879121
# -41.69960474 -54.57627119 951.49253731]
Using Pandas
import pandas as pd
ages = pd.Series([20.3, 30.5, 20.3, 45.5, 50.6, 29.5,
13.4, 140.9])
changes = ages.pct_change() * 100
# 0 NaN
# 1 50.246305
# 2 -33.442623
# 3 124.137931
# 4 11.208791
# 5 -41.699605
# 6 -54.576271
# 7 951.492537
# dtype: float64