I'm new to machine learning,
I have a dataset :
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 :
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()
:
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))