1 回答
TA贡献1851条经验 获得超4个赞
您的数据不是最佳的,因为您永远不会停留在一个位置。我通过添加时间稍微调整了数据,以便Sense Time更容易验证。首先,我将数据读入df_origwith pd.read_clipboard()。然后我们可以继续:
import pandas as pd
import numpy as np
df = df_orig.copy()
# now we need to combine the date and time column, because read_clipboard separates them
df['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 data
df['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 position
df['moved'] = (df['latitude']!=df['latitude'].shift())&(df['longitude']!=df['longitude'].shift())
# Create a marker indicating positions that belong together
df['segment'] = df['moved'].cumsum()
# Now we find the first Sense Time for every group and add it to df
df = 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 Time
df['DeltaT'] = df['Sense Time']-df['Sense Start']
# Last step is to show only one line per segment
results = df.groupby(by='segment').max().loc[:, ['Date', 'latitude', 'longitude', 'DeltaT']]
print(results)
这产生
Date latitude longitude DeltaT
segment
1 1/31/2020 41.834262 -72.708492 00:01:00
2 1/31/2020 41.834285 -72.708569 00:01:00
3 1/31/2020 41.834338 -72.708525 00:01:00
4 1/31/2020 41.834273 -72.708432 00:01:00
5 1/31/2020 41.834482 -72.707898 00:00:00
6 1/31/2020 41.834512 -72.707291 00:00:00
7 1/31/2020 41.834558 -72.708067 00:00:00
8 1/31/2020 41.834132 -72.708273 00:00:00
9 1/31/2020 41.834258 -72.708506 00:01:00
10 1/31/2020 41.834037 -72.707981 00:00:00
11 1/31/2020 41.834083 -72.708680 00:00:00
12 1/31/2020 41.833980 -72.707779 00:00:00
13 1/31/2020 41.834073 -72.708553 00:00:00
14 1/31/2020 41.834415 -72.708167 00:00:00
15 1/31/2020 41.833925 -72.707922 00:00:00
添加回答
举报