How to find percentage change in values of a column using a variable for another column with pandas "category" data type?

Viewed 44

Here's the data frame:

enter image description here

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:

enter image description here

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
1 Answers

Let's say we have some sequence of ages and a refference table between age bins and some value imp:

import pandas as pd
import numpy as np

rng = np.random.default_rng(42)

N = 10    # number of data
data = pd.Series(rng.integers(1, 111, N), name='Ages')

imp = pd.Series(
    data=[18647, 566, 157, 61, 343, 464, 225, 238, 852, 108, 0],
    index=pd.IntervalIndex.from_breaks(range(0,111,10)).rename('pos'),
    name='imp'
)

Now we want to replace each age with the corresponding imp value. To do this we have to categorize ages like imp.index first, see pandas.cut:

m = pd.cut(data, imp.index).map(imp).astype(imp.dtype)

Now we are ready to calculate the relative difference along a data sequence:

output = m.shift(-1)/m - 1    # multiply by 100 if needed

ps.

I'm not sure if it's a good idea to store the data as a percentage, like "10.05%", etc. In my opinion, it's much better to store it as a ratio that can be used in further arithmetic operations. When printing the result on the screen or paper, we can always turn to formatting tools for help:

output.to_frame().style.format('{:.2%}')   
output.to_string(float_format="{:.2%}".format)    
Related