[英]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.