Home > Mobile >  create in pandas matrix rfm
create in pandas matrix rfm

Time:02-14

I'm calculating a rfv table in pandas, but im not able to find a tutorial or post that helps me building the matrix necessary for the graph. The graph i want using this base from matplot each square being the count of clients in that position.

The dataframe im using now has this columns:

COD_CLIENT RECENCY FRE_VAL R FV RFV RFV_Score RFV_Level
59 87 45.45 3 3 33 6 Potential
1846 75 6.00 3 2 32 5 Seleepers
4380 92 37.95 2 3 23 5 Seleepers
object int64 float64 int32 int32 int32 int64 object

What do you guys sugest i do? I already tried using R and FV as columns and rows and using a function but this went badly.

CodePudding user response:

You want to count the number of rows for each (R, FV) pair, so you could try using pandas.DataFrame.groupby and then counting the number of elements in each group by using .size() on the previous result. The resulting code should look like this:

import pandas as pd


>> df = ...  # Assuming your DataFrame has the format you showed

>> df.groupby(["R", "FV"]).size()
R  FV
1  1     2
   2     1
   3     1
2  1     1
   2     1
dtype: int64

>> df.groupby(["R", "FV"]).size().unstack()  # Add unstack to format the result
FV    1    2    3
R                
1   2.0  1.0  1.0
2   1.0  1.0  NaN

This should match your expected output.

  • Related