我需要获取特定Unix时间戳的行号,或者如果没有完全匹配则获取最接近的Unix时间戳的行号。 我尝试了以下方法:
x = df.loc[df['unixTime']==1506448028].index[0]
适用于完全匹配,但如果没有完全匹配,我找不到设置容差的方法或找到最接近的值。
我也尝试过:
x = df.index.get_loc(1506448028, method='nearest', tolerance=10)
但即使确切的密钥在数据框中,它也会引发一个关键错误。
以下是数据的样子:
Out[89]:
unixTime lat long alt
0 1506447200 39.129079 -84.511796 217.958542
1 1506447191 39.129079 -84.511796 217.958542
2 1506447200 39.129079 -84.511796 217.958542
3 1506447558 39.130221 -84.507324 217.958542
4 1506447558 39.130247 -84.507221 217.958542
5 1506447558 39.130260 -84.507169 217.958542
6 1506447558 39.130267 -84.507144 217.958542
7 1506447558 39.128953 -84.511676 215.973724
8 1506447567 39.128991 -84.511545 215.973724
9 1506447570 39.129015 -84.511679 221.638443
10 1506447576 39.128930 -84.511686 221.962585
11 1506447582 39.128912 -84.511712 222.543442
12 1506447588 39.129003 -84.511683 222.282623
13 1506447594 39.129002 -84.511696 225.494690
14 1506447600 39.128912 -84.511724 224.964630
15 1506447606 39.128991 -84.511683 224.597717
16 1506447612 39.128950 -84.511725 225.394653
17 1506447618 39.128953 -84.511740 226.599243
18 1506447624 39.128974 -84.511605 226.195770
19 1506447630 39.128950 -84.511646 225.726181
20 1506447636 39.128931 -84.511735 226.083969
21 1506447642 39.128931 -84.511727 225.133530
22 1506447648 39.128940 -84.511840 226.574783
23 1506447654 39.129015 -84.511668 226.336227
24 1506447660 39.129095 -84.511718 227.314056
25 1506447666 39.129106 -84.511818 227.421555
26 1506447672 39.129064 -84.511947 227.557953
27 1506447681 39.129021 -84.512115 227.557953
28 1506447683 39.128886 -84.512158 227.237656
29 1506447689 39.128941 -84.512043 227.278915
... ... ... ...
4751 1506452685 39.129019 -84.511971 240.232147
4752 1506452689 39.128954 -84.511990 239.229721
4753 1506452696 39.129030 -84.511841 238.480148
4754 1506452699 39.129061 -84.511804 237.872940
4755 1506452699 39.129068 -84.511809 237.377457
4756 1506452702 39.129059 -84.511761 236.937805
4757 1506452708 39.129074 -84.511749 236.517029
4758 1506452714 39.129059 -84.511770 236.134262
4759 1506452720 39.129041 -84.511709 235.667023
4760 1506452726 39.128983 -84.511709 235.206390
4761 1506452732 39.128972 -84.511713 234.759903
4762 1506452732 39.128957 -84.511694 218.916260
4763 1506452733 39.128955 -84.511697 218.885803
4764 1506452734 39.128953 -84.511700 218.858765
4765 1506452735 39.128951 -84.511702 218.833496
4766 1506452736 39.128949 -84.511704 218.811340
4767 1506452737 39.128946 -84.511705 218.792725
4768 1506452738 39.128956 -84.511709 217.345093
4769 1506452739 39.128966 -84.511712 217.287170
4770 1506452740 39.128975 -84.511716 217.250488
4771 1506452741 39.128975 -84.511716 217.185913
4772 1506452742 39.128975 -84.511716 217.162598
4773 1506452743 39.128975 -84.511716 217.177856
4774 1506452744 39.128975 -84.511716 217.164185
4775 1506452745 39.128975 -84.511716 217.164169
4776 1506452746 39.128975 -84.511716 217.145004
4777 1506452747 39.128986 -84.511719 217.197449
4778 1506452748 39.128994 -84.511722 217.215179
4779 1506452749 39.129002 -84.511725 217.219131
4780 1506452750 39.129007 -84.511726 217.221527
[4781 rows x 4 columns]
答案 0 :(得分:1)
使用Series.idxmin()方法:
演示:
In [264]: df
Out[264]:
unixTime lat long alt
0 1506447200 37.629079 -86.011796 216.458542
1 1506447191 37.629079 -86.011796 216.458542
2 1506447200 37.629079 -86.011796 216.458542
3 1506447558 37.630221 -86.007324 216.458542
4 1506447558 37.630247 -86.007221 216.458542
... ... ... ... ...
2994 1506450890 37.634599 -86.015575 197.862915
2995 1506450891 37.634595 -86.015585 198.044250
2996 1506450892 37.634594 -86.015596 199.779419
2997 1506450893 37.634596 -86.015602 199.778015
2998 1506450894 37.634598 -86.015608 199.778320
[2999 rows x 4 columns]
In [268]: (df['unixTime']-1506448028).abs().idxmin()
Out[268]: 311
检查:
In [270]: df.loc[[311]]
Out[270]:
unixTime lat long alt
311 1506448028 37.630432 -86.016418 235.709106
<强>更新强>
In [273]: (df['unixTime']-1506450454).abs().idxmin()
Out[273]: 2723
In [274]: df.loc[[(df['unixTime']-1506450454).abs().idxmin()]]
Out[274]:
unixTime lat long alt
2723 1506450450 37.634045 -86.015777 218.11084