pandas 给数据打标签

import numpy as np
import pandas as pd

df = pd.DataFrame(np.random.randint(0,100,100), columns=['score'])

# 以所在区间作为标签。如 x=5,返回:'[0-10]'
def make_label(x, step=10):
    m = x // step
    return '[{}-{}]'.format(m * step, (m+1) * step)
    
    
#df['level'] = df['score'].map(make_label)
df['level'] = df['score'].map(lambda x:make_label(x,step=10)) # 改变区间长度为15


res = df.groupby('level').size()

print(df.head())
print(res)
posted @ 2016-08-29 11:49  罗兵  阅读(6354)  评论(0编辑  收藏  举报