Search code examples
pythonpandasbins

How to include 0 value when binning


Can you please tell me how to include also 0 here?

data['count_comma'] = pd.cut(data['comma'], bins=[0,6,np.inf], labels=['lt6','ge6'])

Trying as above, I get NaN values when I have no comma (i.e., 0) in texts. It should be included and get value lt6.

Thanks a lot.


Solution

  • Use the include_lowest=True argument.

    import pandas as pd
    data = pd.DataFrame({'comma': [0,1,2,3,4,5,6,7,8]})
    
    data['count_comma'] = pd.cut(data['comma'], 
                                 bins=[0, 6, np.inf], labels=['lt6','ge6'],
                                 include_lowest=True)
    

       comma count_comma
    0      0         lt6
    1      1         lt6
    2      2         lt6
    3      3         lt6
    4      4         lt6
    5      5         lt6
    6      6         lt6
    7      7         ge6
    8      8         ge6