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

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

迭代數據集并更改 Pandas 未來索引中其他值的特征值的更好方法

迭代數據集并更改 Pandas 未來索引中其他值的特征值的更好方法

Go
慕無忌1623718 2023-02-07 09:39:17
我有一個由高速公路上的傳感器記錄的速度數據集,我正在更改未來 2 小時(5 分鐘時間戳的平均速度)的值(正常值為 30 分鐘?,F在的label值是觀察到的 30 分鐘將來)。avg5labelavg5我的數據集具有以下特征和值:我正在通過這種方式進行這種價值轉換:hours_added = datetime.timedelta(hours = 2)for index in data_copy.index:  hours_ahead = data.loc[index, "timestamp5"] + hours_added  result = data_copy[((data_copy["timestamp5"] == hours_ahead) & (data_copy["sensor_id"] == data_copy["sensor_id"].loc[index]))]  if len(result) == 1:    data_copy.at[index, "label"] = result["avg5"]  if(index % 50 == 0):    print(f"Index: {index}")該代碼提前 2 小時查詢并捕獲我現在正在迭代的相同 sensor_id 的結果。如果結果給我帶來了一些東西,我只會更改標簽的值(len(result) == 1).我的數據框有 2950521 個索引,目前我發布這個問題內核運行了超過 24 小時,只達到了 371650 個索引。所以我開始認為我做錯了什么,或者是否有更好的方法來改變這些不需要那么長時間的價值觀。出于復制目的,所需的行為是為avg52 小時前的標簽分配未來 2 小時的相應 sensor_id。為了可重復性,以我的數據集樣本為例,其中包含前 10 個寄存器:
查看完整描述

1 回答

?
慕沐林林

TA貢獻2016條經驗 獲得超9個贊

根據我對您的代碼如何工作的理解,它似乎花費了這么長時間,因為它在 O(n^c) 時間之內運行。我的意思是,對于每個索引,它必須多次遍歷整個數據集以檢查條件。


因此,最好嘗試避免遍歷每個索引的整個數據集——即,使其在 O(n) 線性時間內工作。為此,我將執行以下操作:


import pandas as pd

from pandas import Timestamp

import datetime


data_copy = pd.DataFrame(data = {

    'sensor_id': {

        0: 1385001, 1: 1385001, 2: 1385001, 3: 1385001, 4: 1385001, 5: 1385001,

        6: 1385001, 7: 1385001, 8: 1385001, 9: 1385001},

    'label': {

        0: 50.79999923706055, 1: 52.69230651855469, 2: 50.0, 3: 48.61538314819336,

        4: 48.0, 5: 47.90909194946289, 6: 51.41666793823242, 7: 48.3684196472168,

        8: 49.8636360168457, 9: 48.66666793823242},

    'avg5': {

        0: 49.484848, 1: 51.735294, 2: 51.59375, 3: 49.266666,

        4: 50.135135999999996, 5: 50.5, 6: 50.8, 7: 52.69230699999999,

        8: 50.0, 9: 48.615383},

    'timestamp5': {

        0: Timestamp('2014-08-01 00:00:00'), 1: Timestamp('2014-08-01 00:05:00'),

        2: Timestamp('2014-08-01 00:10:00'), 3: Timestamp('2014-08-01 00:15:00'),

        4: Timestamp('2014-08-01 00:20:00'), 5: Timestamp('2014-08-01 00:25:00'),

        6: Timestamp('2014-08-01 00:30:00'), 7: Timestamp('2014-08-01 00:35:00'),

        8: Timestamp('2014-08-01 00:40:00'), 9: Timestamp('2014-08-01 00:45:00')}})

hours_added = datetime.timedelta(minutes = 40)


# Create a data series that combines the information about sensor_id & timestamp5

sen_time = data_copy['sensor_id'].astype(str) + data_copy['timestamp5'].astype(str)

# Create a dictionary of the corresponding { sensor_id + timestamp5 : avg5 } values

dictionary = pd.Series(data_copy['avg5'].values, sen_time).to_dict()

# Create a data series combining the timestamp5 + 40 mins information

timePlus40 = data_copy['timestamp5'] + hours_added

# Create a mapping column that combines the sensor_id & timestamp5+40mins

sensor_timePlus40 = (data_copy['sensor_id'].astype(str) + timePlus40.astype(str))

# Create a new_label series by mapping the dictionary onto sensor_timePlus40

new_label = sensor_timePlus40.map(dictionary)

# Extract indices where this series has non-NaN values

where = new_label.notnull()

# Replace the values in the 'label' column with only non-NaN new_label values

data_copy.loc[where, 'label'] = new_label.loc[where]

我相信這與@pecey 和@iracebeth_18 在評論中提出的想法類似。


此EDIT ed 版本反映了 OP 的愿望(來自評論)以label僅使用非 NaN 值更新列。


結果如下所示:


> print(data_copy)


   sensor_id      label       avg5          timestamp5

0    1385001  50.000000  49.484848 2014-08-01 00:00:00

1    1385001  48.615383  51.735294 2014-08-01 00:05:00

2    1385001  50.000000  51.593750 2014-08-01 00:10:00

3    1385001  48.615383  49.266666 2014-08-01 00:15:00

4    1385001  48.000000  50.135136 2014-08-01 00:20:00

5    1385001  47.909092  50.500000 2014-08-01 00:25:00

6    1385001  51.416668  50.800000 2014-08-01 00:30:00

7    1385001  48.368420  52.692307 2014-08-01 00:35:00

8    1385001  49.863636  50.000000 2014-08-01 00:40:00

9    1385001  48.666668  48.615383 2014-08-01 00:45:00

將此代碼的速度與您的代碼進行比較會timeit產生更快的運行時間,并且差異只會隨著數據集的增大而增加。


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

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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