python 学习的第六天 补充数据的分组

使用cut函数对数据进行分组

import pandas;
from pandas import read_csv;

df = read_csv("D:\PA\4.15\data.csv", sep='|');

bins = [min(df.cost)-1, 20, 40, 60, 80, 100, max(df.cost)+1];

labels = ['20以下', '20到40', '40到60', '60到80', '80到100', '100以上'];

pandas.cut(df.cost, bins)

pandas.cut(df.cost, bins, right=False)

pandas.cut(df.cost, bins, right=False, labels=labels)

  注意:

cut(series,bins,right=True,labels=NULL)

参数说明: series 需要分组的数据
                   bins 分组的划分数组
                   right 分组的时候,右边是否闭合 TRUE,FALSE
                   labels 分组的自定义标签,可以不自定义

原始数据为:

tel                   |cost
166424556600|2.0
166424557199|5.0
166424561768|75.3
166424569696|20.0
166424569924|97.3
166424579238|3.0
166424581334|100.0
166424589730|77.0
166424591167|5.5
166424598020|50.0
166424598259|28.6
166424606270|10.8
166424632819|76.7
166424635250|84.6
166424641824|10.0

原文地址:https://www.cnblogs.com/manjianlei/p/11266609.html