我在熊猫中为频率表创建了间隔。第一个间隔看起来像这样:(22,29]并从左侧打开-我只希望第一个间隔这样从两侧关闭:[22,29]。我尝试了interval [0] .closed =“ both”但没有用。
intervals = pd.interval_range(start = 22, end = 64, freq = 7)
vek_freq_table = pd.Series([0,0,0,0,0,0], index = intervals)
for x in df.loc[df.loc[:,"c"].notnull(), "c"]:
for y in c_freq_table.index:
if int(x) in y:
c_freq_table.loc[y] +=1
break
您必须使用列表理解(或循环)来构造自己的间隔索引:
intervals = [pd.Interval(i.left, i.right)
if no != 0 else pd.Interval(i.left, i.right, closed='both')
for (no, i) in enumerate(intervals)]
intervals
输出:
[Interval(22, 29, closed='both'),
Interval(29, 36, closed='right'),
Interval(36, 43, closed='right'),
Interval(43, 50, closed='right'),
Interval(50, 57, closed='right'),
Interval(57, 64, closed='right')]
注意:一个更简单的解决方案似乎只是更改第一个元素,例如:
new_first_elem = pd.Interval(intervals[0].left, intervals[0].right, closed='both')
intervals[0] = new_first_elem
但是,此代码引发TypeError:
TypeError: Index does not support mutable operations