定義一個(gè)頻數(shù)統(tǒng)計(jì)函數(shù),會(huì)使用到pd.Interval()
pandas文檔介紹:
class pandas.Interval
Immutable object implementing an Interval, a bounded slice-like interval.
Parameters:
left:orderable scalar
Left bound for the interval.
right:orderable scalar
Right bound for the interval.
closed:{‘right’, ‘left’, ‘both’, ‘neither’}, default ‘right’
# close參數(shù)控制區(qū)間的開閉绍妨,默認(rèn)左開右閉
Whether the interval is closed on the left-side, right-side, both or neither. See the Notes for more detailed explanation.
Sample code:
#將一個(gè)大區(qū)間根據(jù)分組坐標(biāo)分成若干個(gè)小區(qū)間,將組坐標(biāo)作為元素存入列表(從小到大排序)
#list_GROUPX2 為組坐標(biāo)列表硬贯,list_temp_value為數(shù)據(jù)编振,判斷數(shù)據(jù)落入哪個(gè)小區(qū)間中广凸,最終返回元素為各區(qū)間頻數(shù)的列表
def frequency_calculate(self,list_GROUPX2,list_temp_value):
list_zbqj = [] #組坐標(biāo)區(qū)間
for i in range(len(list_GROUPX2)):
if i == 0:
a = pd.Interval(left = float('-inf'), right = list_GROUPX2[i])
list_zbqj.append(a)
elif i == (len(list_GROUPX2)-1):
b = pd.Interval(left = list_GROUPX2[i-1], right = float('inf'),closed = 'neither')
list_zbqj.append(b)
else:
c = pd.Interval(left = list_GROUPX2[i-1], right = list_GROUPX2[i])
list_zbqj.append(c)
list_ps = [] #計(jì)算頻數(shù)
for i in list_zbqj:
CNT = 0
for j in list_temp_value:
if j > i.left:
if j <= i.right:
CNT += 1
list_ps.append(CNT)
return list_ps
蟹蟹