5 Replies Latest reply on Aug 29, 2018 2:31 AM by Sasha Hanna

# Reproduce a Python code to a running sum

Hello,

I have this code in Python and I think it`s possible to change it in a running sum. I want to classify some numbers of clients to a group (A, B or C) and use a count to distribute then in classes.
for example:

I have 5 clients with the most values of discounts, then 15 with discounts above the median and 30 in the median.
I want to classify then in Group A=5, group B=15 and Group C=30.

This code can do this:

count = 0

for index, row in df_pos_filtro_ordenado.iterrows():

count += 1

#print(count , clientes_a)

if (count <= clientes_a):

df_pos_filtro_ordenado.set_value(index, 'GRUPO', 'A')

elif (count <= clientes_b+clientes_a):

df_pos_filtro_ordenado.set_value(index, 'GRUPO', 'B')

elif ((count > clientes_b+clientes_a) & (count <= clientes_total)):

df_pos_filtro_ordenado.set_value(index, 'GRUPO', 'C')

I know Tabpy can do an integration with Python, but I think we can do this code in a calculated field.

• ###### 1. Re: Reproduce a Python code to a running sum

Hi Luana,

Could you attach a sample dataset?

Cheers,

Sasha

• ###### 2. Re: Reproduce a Python code to a running sum

The sample.

We classify the group ABC using the the volume.

• ###### 3. Re: Reproduce a Python code to a running sum

Hi Luana,

Thank you for the sample. Have you tried a conditional statement that looks at the volume and groups the value based on a specific threshold? The important thing here is to order the classes from the highest to  the lowest buckets. In the example above I've used a table calculation to compute the median value, so if the data is updated you will always obtain the most current median. Let me know if this helps.

Cheers,

Sasha

• ###### 4. Re: Reproduce a Python code to a running sum

I don't want to compare the volume with the median. I'm trying to do something like an ABC analysis, so I need group with the 70%,70-90%, 90-100% of the buyers group by amount of volume.

If I have 10 clients, I need 7 of then with the lowest volume and 1 with the highest volume.

customer     volume     ABC

client 1          0.1          C

client 2          0.1          C

client 3          0.3          C

client 4          0.4          C

client 5          0.6          C

client 6          0.6          C

client 7          0.6          C

client 8          0.8          B

client 9          0.8          B

client 10          1          A

• ###### 5. Re: Reproduce a Python code to a running sum

Hi Luana,

Have you already tried the following adapted formula?

If SUM([Volume])<0.7 then 'C'

ELSEIF SUM([Volume])>=0.7 AND SUM([Volume])<0.9 then 'B'

ELSE 'A'

END

Cheers,

Sasha