python - How do I quantize data in pandas? -
i have dataframe
a = pd.dataframe(a.random.random(5, 10), columns=['col1','col2','col3','col4','col5'])
i'd quantize specific column, col4
, according set of thresholds (the corresponding output integer 0 number of levels). there api that?
most pandas objects compatible numpy functions. use numpy.digitize
:
import pandas pd = pd.dataframe(pd.np.random.random((5, 5)), columns=['col1','col2','col3','col4','col5']) # col1 col2 col3 col4 col5 #0 0.523311 0.266401 0.939214 0.487241 0.582323 #1 0.274436 0.761046 0.155482 0.630622 0.044595 #2 0.505696 0.953183 0.643918 0.894726 0.466916 #3 0.281888 0.621781 0.900743 0.339057 0.427644 #4 0.927478 0.442643 0.541234 0.450761 0.191215 pd.np.digitize( a.col4, bins = [0.3,0.6,0.9 ] ) #array([1, 2, 2, 1, 1])
Comments
Post a Comment