Home > Net >  How to replace data in a certain range with a variable?
How to replace data in a certain range with a variable?

Time:05-09

I'm new to machine learning,

I have a dataset :

enter image description here

I want to create a "bucket" :

[0-25] = A

[26-50] = B

[51-75] = C

[76-100] = D

I tried panda.cut() :

bins = [-1, 26, 51, 76, 100]    
labels = ["A", "B", "C", "D"]    
dataset['UAS'] = pd.cut(dataset['UAS'], bins=bins, labels=labels)

Result :

enter image description here

It only works on a 1-dimensional array. Any tips/lib to "cut" all columns simultaneously without repeating the code?

Thanks a lot.

** tried apply() :

enter image description here

CodePudding user response:

Use:

#only numeric columns
cols = dataset.select_dtypes(np.number).columns
#pass cut for columns from list
dataset[cols] = dataset[cols].apply(lambda x: pd.cut(x, bins=bins, labels=labels))
  • Related