Here's the data frame:
df_temp = pd.DataFrame({'pos': {0: '1-10',
1: '11-20',
2: '21-30',
3: '31-40',
4: '41-50',
5: '51-60',
6: '61-70',
7: '71-80',
8: '81-90',
9: '91-100',
10: '101-110'},
'imp': {0: 18647,
1: 566,
2: 157,
3: 61,
4: 343,
5: 464,
6: 225,
7: 238,
8: 852,
9: 108,
10: 0}})
df_temp
In my notebook, df.dtypes show:
I don't know how to put such data type through a dict.
Now if "pos" of x goes up from say 73 to 15. Required function:
def percent_change(73, 15):
=> 238*(x/100) = 566 #imp at 71-80 = 238 and 11-20 = 566
=> x = (566*100)/238
=> x = 100 - x # Mayby? To show only change?
return x
Edit: My embarrassing solution, simply did string operation:
def p_change(x, y, df):
if len(str(x)) == 1:
x = 0
elif int(str(x)[1]) == 0:
x = int(str(x)[0]) - 1
else:
x = int(str(x)[0])
if len(str(y)) == 1:
y = 0
elif int(str(y)[1]) == 0:
y = int(str(y)[0]) - 1
else:
y = int(str(y)[0])
x = int(df.loc[x, 'Impressions'])
y = int(df.loc[y, 'Impressions'])
p_change = print('Percentage change: ' + str(round(((y*100)/x - 100), 2)) + '%')
return p_change

