如何查找 Panda 数据框中每个位置花费的时间?

这是我给定的数据框。


        Date        latitude    longitude   Sense Time

0   1/31/2020   41.83426175 -72.70849209    1/31/2020 20:16

1   1/31/2020   41.83426175 -72.70849209    1/31/2020 20:16

2   1/31/2020   41.83428482 -72.70856874    1/31/2020 20:17

3   1/31/2020   41.83428482 -72.70856874    1/31/2020 20:17

4   1/31/2020   41.83433778 -72.70852501    1/31/2020 20:22

5   1/31/2020   41.83433778 -72.70852501    1/31/2020 20:22

6   1/31/2020   41.83427319 -72.70843216    1/31/2020 20:28

7   1/31/2020   41.83427319 -72.70843216    1/31/2020 20:28

8   1/31/2020   41.83448205 -72.70789807    1/31/2020 20:33

9   1/31/2020   41.83451187 -72.70729114    1/31/2020 20:34

10  1/31/2020   41.83455839 -72.70806683    1/31/2020 20:48

11  1/31/2020   41.83413174 -72.70827285    1/31/2020 20:50

12  1/31/2020   41.83425776 -72.70850601    1/31/2020 21:25

13  1/31/2020   41.83425776 -72.70850601    1/31/2020 21:25

14  1/31/2020   41.83403703 -72.70798106    1/31/2020 23:11

15  1/31/2020   41.83408303 -72.70867975    1/31/2020 23:19

16  1/31/2020   41.83398011 -72.70777882    1/31/2020 23:25

17  1/31/2020   41.83407303 -72.70855327    1/31/2020 23:29

18  1/31/2020   41.83441461 -72.70816693    1/31/2020 23:32

19  1/31/2020   41.83392464 -72.7079223     1/31/2020 23:32

如何找出在每个位置(纬度、经度)花费的总时间,然后将其添加到数据框中的新列?


慕容森
浏览 101回答 1
1回答

繁花不似锦

您的数据不是最佳的,因为您永远不会停留在一个位置。我通过添加时间稍微调整了数据,以便Sense Time更容易验证。首先,我将数据读入df_origwith pd.read_clipboard()。然后我们可以继续:import pandas as pdimport numpy as npdf = df_orig.copy()# now we need to combine the date and time column, because read_clipboard separates themdf['Sense Time'] = pd.to_datetime(df['Date'] + " " +df['Time'])df=df.drop(['Sense', 'Time'], axis=1)# next step we add an increasing number of minutes to Sense Time to get more reasonable datadf['Sense Time'] = df['Sense Time']+pd.to_timedelta(range(0, df.shape[0]), unit='min')# now we try to determine if we have moved or stayed at the same positiondf['moved'] = (df['latitude']!=df['latitude'].shift())&(df['longitude']!=df['longitude'].shift())# Create a marker indicating positions that belong togetherdf['segment'] = df['moved'].cumsum()# Now we find the first Sense Time for every group and add it to dfdf = pd.concat([df, df.groupby('segment').transform('first')[['Sense Time']].rename(columns={'Sense Time': 'Sense Start'})], axis=1)# DeltaT is the time difference between Sense Start and Sense Timedf['DeltaT'] = df['Sense Time']-df['Sense Start']# Last step is to show only one line per segmentresults = df.groupby(by='segment').max().loc[:, ['Date', 'latitude', 'longitude', 'DeltaT']]print(results)这产生              Date   latitude  longitude   DeltaTsegment                                          1        1/31/2020  41.834262 -72.708492 00:01:002        1/31/2020  41.834285 -72.708569 00:01:003        1/31/2020  41.834338 -72.708525 00:01:004        1/31/2020  41.834273 -72.708432 00:01:005        1/31/2020  41.834482 -72.707898 00:00:006        1/31/2020  41.834512 -72.707291 00:00:007        1/31/2020  41.834558 -72.708067 00:00:008        1/31/2020  41.834132 -72.708273 00:00:009        1/31/2020  41.834258 -72.708506 00:01:0010       1/31/2020  41.834037 -72.707981 00:00:0011       1/31/2020  41.834083 -72.708680 00:00:0012       1/31/2020  41.833980 -72.707779 00:00:0013       1/31/2020  41.834073 -72.708553 00:00:0014       1/31/2020  41.834415 -72.708167 00:00:0015       1/31/2020  41.833925 -72.707922 00:00:00
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python