gpt4 book ai didi

python - 索引包含开区间的 Pandas 数据帧

转载 作者:行者123 更新时间:2023-12-01 02:07:52 24 4
gpt4 key购买 nike

我如何索引如下所示的数据框:

i   BDia         
0 (0.025, 0.05] 0.006726
(0.05, 0.075] 0.006650
(0.075, 0.1] 0.005531
(0.1, 0.125] 0.005792
(0.125, 0.15] 0.006576
(0.15, 0.175] 0.000141
1 (0.025, 0.05] 0.007256
(0.05, 0.075] 0.006929
(0.075, 0.1] 0.006551
(0.1, 0.125] 0.007238
(0.125, 0.15] 0.007008
(0.15, 0.175] 0.006658

其中iBDia是多级索引。例如,我如何选择开区间为 (0.025, 0.05] 的所有值?

最佳答案

我认为你需要Interval然后选择像 MultiIndexloc 或使用 slicers :

a = pd.Interval(left=0.025, right=0.05)
#or
#a = pd.IntervalIndex.from_tuples([(0.025, 0.05)])[0]
print (a)
(0.025, 0.05]

df = df.loc[:, [a]]
print (df)
i
0 (0.025, 0.05] 0.006726
1 (0.025, 0.05] 0.007256
Name: col, dtype: float64
<小时/>
a = df.loc[:, a]
print (a)
i
0 0.006726
1 0.007256
Name: col, dtype: float64
<小时/>
idx = pd.IndexSlice
a = df.loc[idx[0, a]]
print (a)
0.006726

关于python - 索引包含开区间的 Pandas 数据帧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48887698/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com