亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何查找 Panda 數據框中每個位置花費的時間?

如何查找 Panda 數據框中每個位置花費的時間?

慕容森 2023-10-05 16:55:34
這是我給定的數據框。        Date        latitude    longitude   Sense Time0   1/31/2020   41.83426175 -72.70849209    1/31/2020 20:161   1/31/2020   41.83426175 -72.70849209    1/31/2020 20:162   1/31/2020   41.83428482 -72.70856874    1/31/2020 20:173   1/31/2020   41.83428482 -72.70856874    1/31/2020 20:174   1/31/2020   41.83433778 -72.70852501    1/31/2020 20:225   1/31/2020   41.83433778 -72.70852501    1/31/2020 20:226   1/31/2020   41.83427319 -72.70843216    1/31/2020 20:287   1/31/2020   41.83427319 -72.70843216    1/31/2020 20:288   1/31/2020   41.83448205 -72.70789807    1/31/2020 20:339   1/31/2020   41.83451187 -72.70729114    1/31/2020 20:3410  1/31/2020   41.83455839 -72.70806683    1/31/2020 20:4811  1/31/2020   41.83413174 -72.70827285    1/31/2020 20:5012  1/31/2020   41.83425776 -72.70850601    1/31/2020 21:2513  1/31/2020   41.83425776 -72.70850601    1/31/2020 21:2514  1/31/2020   41.83403703 -72.70798106    1/31/2020 23:1115  1/31/2020   41.83408303 -72.70867975    1/31/2020 23:1916  1/31/2020   41.83398011 -72.70777882    1/31/2020 23:2517  1/31/2020   41.83407303 -72.70855327    1/31/2020 23:2918  1/31/2020   41.83441461 -72.70816693    1/31/2020 23:3219  1/31/2020   41.83392464 -72.7079223     1/31/2020 23:32如何找出在每個位置(緯度、經度)花費的總時間,然后將其添加到數據框中的新列?
查看完整描述

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


查看完整回答
反對 回復 2023-10-05
  • 1 回答
  • 0 關注
  • 138 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號