简体   繁体   中英

How to search a certain value in series python

I've got a series: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

and I believe there is a value '4.74036126519e-07' in it

I try some methods to find the value:

p[p =='value']

or function:

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

but they return nothing

strangely, when I call:

 p[p ==0]

it can return the index

I wanna ask why and how to find value in series properly

code:

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 is a dataframe contain users' location information (longtitude latitude)

and use haversine to count the distance between their trips

then use a for loop to append together the distance :p

actually the number i try to find is not so important . i cannot get a result from searching other values in the series either like 353.267439 (the first element)

This adds the rounding in you checking function:

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

n is the number of digits the number will be rounded to.

You can test it using a simple script like this one

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]

Where series.txt is a text file with the data you posted (with one removed empty line). The above will print the correct indexes - it mimics the situation where rounding is up to the 3 decimal which is the precision of the input in check - except for the middle element.

Similarly it will work if the values in check have some trailing numbers,

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

But it will not - except for the exact one - if you increase the precision past the lowest one,

check = [353.267,0.706518,24.292]
print [find(res, x, 7) for x in check]
# yields [None, 28, None]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM