我有一個我在這里在線抓取的 .csv:https : //marketplace.spp.org/file-browser-api/download/generation-mix-historical?path=/GenMix_2017.csv
第一列是日期/時間,分為 5 分鐘間隔(軍用時間)。我需要確保日期僅適用于“2017”,因為 .csv 末尾有一些資料來自 2018 年。我希望能夠捕獲所有資料,但只能以一小時為增量。
例如在那個 .csv 中,這將是:
2017-01-01T06:00:00Z到2017-01-01T06:55:00Z這是12行。
這只是 2017-01-01 在時間開始的情況:6:00:00所有其他時間都從0:00:00
我在想我可能只能以 12 步增量迭代“2017”資料以獲得小時塊,然后一旦它運行了 12*24 次,它就會重置,不知道如何做到這一點。
但也不確定這是否是未來用例的一個好主意,可能是時代變了或資料丟失。努力確保它在使用幾年后不會損壞。可以肯定地說,生產這些資料的公司將繼續以同樣的方式生產它,但我想你永遠不會知道。
這是我到目前為止所擁有的:
# puts api call into a pandas dataframe
energy_data = pd.read_csv('https://marketplace.spp.org/file-browser-api/download/generation-mix-historical?path=/GenMix_2017.csv')
# puts the date/time field into proper float64 format
energy_data['GMT MKT Interval'] = pd.to_datetime(energy_data['GMT MKT Interval'])
# ensures that the entire dataframe can be treated as time series data
energy_data.set_index('GMT MKT Interval', inplace = True)
uj5u.com熱心網友回復:
使用resample_sum:
df = pd.read_csv('GenMix_2017.csv', parse_dates=['GMT MKT Interval'],
index_col='GMT MKT Interval')
out = df.resample('H').sum()
輸出:
>>> out
Coal Market Coal Self Diesel Fuel Oil Hydro Natural Gas ... Waste Disposal Services Wind Waste Heat Other Average Actual Load
GMT MKT Interval ...
2017-01-01 06:00:00 00:00 34104.7 159041.4 0.0 3220.5 35138.3 ... 113.8 57517.0 0 431.3 303688.602
2017-01-01 07:00:00 00:00 32215.4 156570.6 0.0 3326.3 33545.2 ... 132.9 63397.0 0 422.9 304163.427
2017-01-01 08:00:00 00:00 29604.7 152379.6 0.0 3246.0 33851.4 ... 133.2 64230.5 0 358.1 300871.117
2017-01-01 09:00:00 00:00 28495.9 149474.0 0.0 2973.1 35171.5 ... 131.9 65860.7 0 344.5 298908.514
2017-01-01 10:00:00 00:00 29304.8 146561.1 0.0 3161.2 34315.4 ... 133.8 67882.8 0 340.9 299825.531
... ... ... ... ... ... ... ... ... ... ... ...
2018-01-01 01:00:00 00:00 36071.3 216336.8 55.2 16093.1 93466.6 ... 140.4 75547.5 0 327.6 463542.027
2018-01-01 02:00:00 00:00 35339.9 213596.9 55.2 14378.4 97397.7 ... 114.6 75277.5 0 325.4 459252.079
2018-01-01 03:00:00 00:00 35051.4 217333.2 55.2 12334.3 96351.1 ... 107.3 69376.7 0 328.1 453214.866
2018-01-01 04:00:00 00:00 35220.7 220868.9 53.2 8520.8 98404.2 ... 116.9 60699.7 0 328.5 446139.366
2018-01-01 05:00:00 00:00 35392.1 223590.8 52.2 8980.9 103893.6 ... 131.1 48453.0 0 329.8 439107.888
[8760 rows x 12 columns]
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/372781.html
