Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

pd.cut in pandas

>>> pd.qcut(range(5), 4, labels=False)
array([0, 0, 1, 2, 3])
Comment

pd.cut in pandas

>>> pd.qcut(range(5), 3, labels=["good", "medium", "bad"])
... 
[good, good, medium, bad, bad]
Categories (3, object): [good < medium < bad]
Comment

Dataframe cut

import pandas as pd

#设置切分区域
listBins = [0, 10, 20, 30, 40, 50, 60, 1000000]

#设置切分后对应标签
listLabels = ['0_10','11_20','21_30','31_40','41_50','51_60','61及以上']

#利用pd.cut进行数据离散化切分
"""
pandas.cut(x,bins,right=True,labels=None,retbins=False,precision=3,include_lowest=False)
x:需要切分的数据
bins:切分区域
right : 是否包含右端点默认True,包含
labels:对应标签,用标记来代替返回的bins,若不在该序列中,则返回NaN
retbins:是否返回间距bins
precision:精度
include_lowest:是否包含左端点,默认False,不包含
"""
df['fenzu'] = pd.cut(df['data'], bins=listBins, labels=listLabels, include_lowest=True)
Comment

PREVIOUS NEXT
Code Example
Python :: removing stop words from the text 
Python :: numpy.sort 
Python :: how to loop through an array in python 
Python :: python power of e 
Python :: django create multiple objects 
Python :: python sandbox 
Python :: round python print 
Python :: python re.split() 
Python :: os module in python 
Python :: string manipulation in python 
Python :: python selenium driver 
Python :: show percentage in seaborn countplot site:stackoverflow.com 
Python :: keras transfer learning 
Python :: pytest monkeypatch 
Python :: for loop in python 
Python :: lambda functions python 
Python :: Find the path of python executable 
Python :: scrapy with selenium 
Python :: numpy datatime to string 
Python :: what is xarray 
Python :: try except in list comprehension 
Python :: pandas dummy classification data 
Python :: concatenate strings and int python 
Python :: python iterator 
Python :: swap list 
Python :: get min of list python 
Python :: prompt python 
Python :: pk django 
Python :: dataframe names pandas 
Python :: Python Map Function Syntax 
ADD CONTENT
Topic
Content
Source link
Name
2+1 =