PANAS SHAPING: I want to create a program that changes the customer's cognitive stage based on the actions I have taken with the customer.

Asked 2 years ago, Updated 2 years ago, 47 views

I am not good at calculating the vertical direction of the pandas, so please let me know.

I'd like to create data for simulation, but I'm going to do five things for each customer.Assume that these five actions will raise the level of customer awareness.In the case of action 1 to action 5, the customer stage is divided into six stages, 0 to 5, and in the initial state, the customer stage is first zeroed.The table is in chronological order.

·When the customer stage is 0, the stage goes up to 1
when action 1 is implemented. ·When the customer stage is 1, if action 2 is implemented, the stage will go up to 2
·When the customer stage is 2, if action 3 is implemented, the stage will go up to 3
·When the customer stage is 3, if action 4 is implemented, the stage will go up to 4
·When the customer stage is 4, if action 5 is implemented, the stage will go up to 5

I think the customer stage will remain at 5.
Also, even if the customer stage is 1 and it's not something to go up,
I believe that the customer stage will be maintained.

In other words, if you have an initial table (file) like the one below,

When the file appears,

I came up with the following code, which I know is ignoring the customer's ID and returning to zero after the customer stage is 1.
I'm sorry to trouble you, but I appreciate your cooperation.

df["Customer Stage 2"] = df["Customer Stage"].shift(1).fillna(0)
df.loc[(df["1"]==1), "Customer Stage 2"]=1
df["Customer Stage 3"] = df["Customer Stage 2"].shift(1).fillna(0)
df.loc [(df["2"]==1)&(df["Customer Stage 3"]==1), "Customer Stage 3"]=2
df["Customer Stage 4"] = df["Customer Stage 3"].shift(1).fillna(0)
df.loc [(df["3"]==1)&(df["Customer Stage 4"]==2), "Customer Stage 4"]=3
df["Customer Stage 5"] = df["Customer Stage 4"].shift(1).fillna(0)
df.loc [(df["4"]==1)&(df["Customer Stage 5"]==3), "Customer Stage 5"]=4
df["Customer Stage 6"] = df["Customer Stage 5"].shift(1).fillna(0)
df.loc [(df["5"]==1)&(df["Customer Stage 6"]==4), "Customer Stage 6"]=5
df.loc [df["Customer Stage 2"] == 1, "Customer Stage" = 1
df.loc [df["Customer Stage 3"] == 2, "Customer Stage"] = 2
df.loc [df["Customer Stage 4"] == 3, "Customer Stage"] = 3
df.loc [df["Customer Stage 5"] == 4, "Customer Stage"] = 4
df.loc [df["Customer Stage 6"] == 4, "Customer Stage"] = 5
df=df.Customer op('Customer Stage 2', 'Customer Stage 3', 'Customer Stage 4', 'Customer Stage 5', 'Customer Stage 6', Axis=1)

python python3 pandas

2022-09-30 19:26

2 Answers

 from functools import redundancy

df['Customer Stage'] =(
  df.groupby('Customer', sort=False).apply(lambdax:
    pd.DataFrame(
      reduce(lambdaa, b:a+[a[-1]+(b-a[-1])==1),
             x.filter(regex=r'^behavior')
              .multiple(range(1,6),axis=1).sum(axis=1).values,
             [0][1:], index=x.index)))

print(df.to_markdown(index=False))


2022-09-30 19:26

Almost Python, not like Pandas

import pandas as pd
df = pd.DataFrame([
       ['A', 0, 0, 0, 0, 0],
       ['A', 1, 0, 0, 0, 0],
       ['A', 0, 0, 0, 1, 0],
       ['B', 0, 1, 0, 0, 0],
       ['B', 1, 0, 0, 0, 0],
       ['B', 0, 1, 0, 0, 0],
       ['A', 0, 1, 0, 0, 0],
       ['A', 0, 0, 1, 0, 0],
       ['A', 0, 0, 0, 1, 0],
       ['A', 0, 0, 0, 0, 1],
       ['A', 0, 0, 0, 0, 0],
    ],
    columns=['Customer', 'Action 1', 'Action 2', 'Action 3', 'Action 4', 'Action 5')

cli=dict.fromkeys(df['customer'].unique(), 0)
fork, *a indf.itertuples (index=False, name=None):
    s=cli[k]
    if s<5 and a[s]>=1:#while or if
        s+ = 1
        cli[k]=s
    print(k,*a,s)

This code was written like Pandas.

  • I'm running it with colab, so (row>0) I've written it twice, but if it's a relatively new Python, it might be possible to use walrus operators
  • selectors is the inverse of it tools.compress, a function that filters from data. If pandas/NumPy has similar features, I want to replace them, but I can't find them
def selectors (data, interable):
    it=iter(iterable)
    n = next(it)
    For dind data:
        b=d==n
        field band 1 or 0
        if b: n = next(it, None)

# (`df.v` is a work indicating action n, so you can delete it after processing.)
df['v'] = df.loc[:, 'Action 1': 'Action 5'.apply(lambd row:
        (row>0).argmax() if(row>0).any() else-1,axis=1)
df['Stage'] = df.groupby('Customer').apply(lambdasdf:
        pd.Series(selectors(sdf.v,range(5)),index=sdf.index).cumsum()) .droplevel(0)

display(df.drop(columns='v'))


2022-09-30 19:26

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.