-1

I have a data with 1000 rows and 2 columns. One column with CustomerID and other with values. I need to create a function to bin the values in 5 groups. Binning process I need to use is as follows. All the values=1 will be given a score=1. For remaining values their mean will be taken and the values below the mean will be given a score=2. Further for the remaining values(not scored) their mean will be taken and the values below the mean will be given a score=3. And soo on.

yponde
  • 61
  • 5

1 Answers1

0

Assuming after 5 bins scoring is not calculated, remaining items will have score 0.

import pandas as pd
import numpy as np

cid = np.arange(1,1001)
score = np.zeros(1000, dtype=int)
values = np.random.randint(0,100,1000)

df = pd.DataFrame({'CID':cid, 'Values':values, 'Score':score})

df.loc[df['Values'] == 1, 'Score'] = 1

for i in range(2,6):
    mean = df.loc[df['Score'] ==0, 'Values'].mean()
    df.loc[(df['Score'] == 0) & (df['Values']<mean), 'Score'] = i

print(df)
SubhashR
  • 141
  • 7