![](/img/trans.png)
[英]A way to iterate through rows and columns (in a panda data frame), select rows and columns based on conditions to put into panda another data frame
[英]What is the safest way to iterate through a panda data frame that has rows for the day broken into minutes?
我有一個.csv,我在這里在線抓取: https://marketplace.spp.org/file-browser-api/download/generation-mix-historical?path=%2FGenMix_2017.Z6328CB5675AFFE84
第一列是日期/時間,並分為 5 分鍾間隔(軍用時間)。 我需要確保日期僅適用於“2017”,因為 2018 年的.csv 末尾有一些數據。我希望能夠捕獲所有數據,但只能以一小時為增量。
例如,在 that.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=%2FGenMix_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)
使用resample_sum
:
df = pd.read_csv('GenMix_2017.csv', parse_dates=['GMT MKT Interval'],
index_col='GMT MKT Interval')
out = df.resample('H').sum()
Output:
>>> 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]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.