Python Forum
Inplace=true with a function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Inplace=true with a function
#1
Hi everyone,

I have written a function and I want it to:

1) have inplace=True so it overrides what is in the df
2) loops through all variables in the df

My function is below:
def binning(var):
    bin_list = []
    for i in var:
        if i <= np.percentile(var, 25): bin_list.append(1)
        elif i <= np.percentile(var, 50): bin_list.append(2)
        elif i <= np.percentile(var, 75): bin_list.append(3)
        else: bin_list.append(4)
    return bin_list

print(binning(credit["PAY_0"].values))
I have about 20 variables I'd like it to go through.

I appreciate any help.

Thanks
Reply
#2
Not an answer, but I think numpy.digitize() or pandas.cut() could be used here.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020