'How to replace data in a certain range with a variable?

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



Solution 1:[1]

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))

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1