如何在python系列中搜索某个值

问题描述 投票:0回答:1

我有一个系列:p

0          353.267439
1          388.483605
2            0.494685
3            1.347499
4          404.202001
5            6.163468
6           29.782820
7           28.972926
8            2.822725
9            0.000000
10           1.309716
11           1.309716
12           0.000000
13           0.000000
14           0.000000
15           0.000000
16          63.199779
17          62.669258
18           0.306850
19           0.000000
20          28.218308
21          32.078732
22           4.394789
23           0.995053
24         236.355502
25         172.802915
26           1.207798
27           0.174134
28           0.706518
29           0.922744

1666374      0.000000
1666375      0.000000
1666376      0.000000
1666377      0.000000
1666378      0.033375
1666379      0.033375
1666380      0.118138
1666381      0.118138
1666382     12.415525
1666383     12.415525
1666384     24.252089
1666385      0.270588
1666386     24.292072
1666387     12.415525
1666388     12.415525
1666389      0.000000
1666390      0.000000
1666391      0.000000
1666392      0.118138
1666393      0.118138
1666394      0.118138
1666395      0.000000
1666396      0.000000
1666397      0.000000
1666398      0.000000
1666399      0.000000
1666400      0.118138
1666401      0.000000
1666402      0.118138
1666403      0.118138
Name: Dis, Length: 1666404, dtype: float64

而且我相信其中有一个价值'4.74036126519e-07'

我尝试了一些方法来找到值:

p[p =='value']

或功能:

def find(s, el):
    for i in s.index:
        if s[i] == el: 
            return i
    return None

但他们没有回报

奇怪的是,当我打电话时:

 p[p ==0]

它可以返回索引

我想问为什么以及如何正确地找到系列价值

码:

def haversine_np(lon1, lat1, lon2, lat2):
lon1, lat1, lon2, lat2 = map(np.radians, [lon1, lat1, lon2, lat2])
dlon = lon2 - lon1
dlat = lat2 - lat1
a = np.sin(dlat/2.0)**2 + np.cos(lat1) * np.cos(lat2) * np.sin(dlon/2.0)**2
c = 2 * np.arcsin(np.sqrt(a))
km = 6367 * c
return km

def DisM(df,ID):
df_user=df.loc[df['UserID'] == ID]
p= haversine_np(df_user.Longitude.shift(), df_user.Latitude.shift(), df_user.ix[1:, 'Longitude'], df_user.ix[1:, 'Latitude'])
p=p.iloc[1:]
p=p.rename("Dis")
return (p)

p = DisM(df,1)
for num in np.arange(2,4861):
   p= p.append(DisM(df,num))

p=p.reset_index(drop=True)

df是包含用户位置信息(经度纬度)的数据框

并使用hasrsine计算他们的旅行之间的距离

然后使用for循环将距离附加在一起:p

实际上,我试图找到的数字并不那么重要。我无法通过搜索系列中的其他值得到结果,如353.267439(第一个元素)

python search series
1个回答
0
投票

这会在您检查函数中添加舍入:

def find(s, el, n):
    for i in range(len(s)):
        if round(s[i],n) == round(el,n):
            return i
    return None

n是数字将舍入到的位数。

您可以使用像这样的简单脚本来测试它

series = []
with open('series.txt','r') as f:
    for line in f:
        series.append(line.strip().split())

res = [float(x[1]) for x in series]

check = [353.267,0.706518,24.292]
print [find(res, x, 3) for x in check]
# yields [0, 28, 42]

其中series.txt是一个文本文件,其中包含您发布的数据(其中一个已删除空行)。上面将打印正确的索引 - 它模仿舍入达到3位小数的情况,这是check中输入的精度 - 除了中间元素。

同样,如果check中的值有一些尾随数字,它将起作用,

check = [353.2671111,0.7065181111,24.292111]
print [find(res, x, 3) for x in check]
# yields [0, 28, 42]

但它不会 - 除了确切的一个 - 如果你将精度提高到最低值之后,

check = [353.267,0.706518,24.292]
print [find(res, x, 7) for x in check]
# yields [None, 28, None]
© www.soinside.com 2019 - 2024. All rights reserved.