我有一个这样的DataFrame
a = pd.DataFrame(a.random.random(5, 10), columns=['col1','col2','col3','col4','col5'])
我想col4
根据一组阈值来量化特定的列,例如,(对应的输出可以是0到级别数之间的整数)。是否有用于此的API?
大多数熊猫对象与numpy函数兼容。我会用numpy.digitize
:
import pandas as pd
a = 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])
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句