📜  Python Pandas – 返回区间的中点

📅  最后修改于: 2022-05-13 01:55:02.553000             🧑  作者: Mango

Python Pandas – 返回区间的中点

在本文中,我们将讨论如何在Python编程语言中使用 pandas 返回给定区间的中点。

Pandas Interval.mid属性用于查找区间的中点。它返回区间的中点。中点是一个区间的两点的中心。它是一个与区间上界和下界等距的点。

示例 1:

导入 Pandas 包并创建一个区间。 Interval.left用于检索间隔的左边界。 Interval.right用于检索区间的右边界。 Interval.mid用于查找区间的中点。 Interval.length用于查找区间的长度。

Python3
# import packages
import pandas as pd
  
# creating 1st interval
interval1 = pd.Interval(0, 10)
  
print('The interval\'s left bound is : ' + str(interval1.left))
print('The interval\'s right bound is : ' + str(interval1.right))
print('The length of the interval is : ' + str(interval1.length))
print('mid point of the interval is : '+str(interval1.mid))
print(interval1.closed)


Python3
# import packages
import pandas as pd
  
# creating 1st interval
interval1 = pd.Interval(0, 10)
  
print('The interval\'s left bound is : ' + str(interval1.left))
print('The interval\'s right bound is :  : ' + str(interval1.right))
print('The length of the interval is : ' + str(interval1.length))
print('mid point of the interval is : ' + str((interval1.left+interval1.right)/2))
print(interval1.closed)


Python3
# import packages
import pandas as pd
  
# creating intervals
# an interval closed on both sides a<=x<=b
interval1 = pd.Interval(0, 10, closed='both')
  
# an open interval a


输出:

The interval's left bound is : 0
The interval's right bound is : 10
The length of the interval is : 10
mid point of the interval is : 5.0
right

特例:

除了使用 interval.mid 属性,我们还可以使用公式 (Interval.left+Interval.right)/2 进行检查。返回相同的结果。

Python3

# import packages
import pandas as pd
  
# creating 1st interval
interval1 = pd.Interval(0, 10)
  
print('The interval\'s left bound is : ' + str(interval1.left))
print('The interval\'s right bound is :  : ' + str(interval1.right))
print('The length of the interval is : ' + str(interval1.length))
print('mid point of the interval is : ' + str((interval1.left+interval1.right)/2))
print(interval1.closed)

输出:

The interval's left bound is : 0
The interval's right bound is : 10
The length of the interval is : 10
mid point of the interval is : 5.0
right

示例 2:

在此示例中,我们使用 closed 参数,一种情况是关闭是“两者”,另一种情况是关闭是既不或开区间。闭区间有其最左边界和最右边界。开区间不包括其最左边界和最右边界。但是在最新版本的 pandas 中,两种情况的结果是相同的。

Python3

# import packages
import pandas as pd
  
# creating intervals
# an interval closed on both sides a<=x<=b
interval1 = pd.Interval(0, 10, closed='both')
  
# an open interval a

输出:

interval1's left bound is : 0
interval1's right bound is : 10
The length of interval1 is : 10
mid point of the interval1 is : 5.0
both
interval2's left bound is : 15
interval2's right bound is :  25
The length of interval2 is : 10
mid point of the interval1 is : 20.0
neither