定义一个频数统计函数,会使用到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参数控制区间的开闭,默认左开右闭
Whether the interval is closed on the left-side, right-side, both or neither. See the Notes for more detailed explanation.
Sample code:
#将一个大区间根据分组坐标分成若干个小区间,将组坐标作为元素存入列表(从小到大排序)
#list_GROUPX2 为组坐标列表,list_temp_value为数据,判断数据落入哪个小区间中,最终返回元素为各区间频数的列表
def frequency_calculate(self,list_GROUPX2,list_temp_value):
list_zbqj = [] #组坐标区间
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 = [] #计算频数
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
蟹蟹
网友评论