Search code examples
pythonpandasbinning

How to bin values, while ignoring others that fulfill a certain criteria in a Dataframe?


My Dataframe consists of non-negative values except for missing values which have been encoded as negative. So I want to bin on every column while excluding the negative values.

So far qcut on [df>0] puts Nan where previously there were negative values, and that is not what I want as I want to retain those values(they have meaning).

dat[i]=pd.qcut(dat[i][dat[i]>0], 10,labels = False, duplicates='drop') 

Solution

  • Your desired input and output are still a little unclear to me (putting them into the question would probably help). But I think you can achieve what you want simply by creating a temporary column and filling the NaN values with the original column.

    Starting with your original qcut code, but assigning it to a new column:

    dat['temp'] = pd.qcut(dat[i][dat[i]>0], 10, labels=False, duplicates='drop')
    dat[i] = dat['temp'].fillna(df[i])
    del df['temp']