gpt4 book ai didi

python - 循环遍历多个数据帧并创建日期时间索引然后加入数据帧

转载 作者:行者123 更新时间:2023-12-04 07:38:41 24 4
gpt4 key购买 nike

我有 9 个不同长度但格式相似的数据帧。每个数据帧都有一个 year , month , 和 day日期跨越 1/1/2009-12/31/2019 的列,但有些数据帧丢失了几天的数据。我想用日期时间索引构建一个大型数据框,但我无法创建一个循环来将年、月和日列转换为每个数据框的日期时间索引,并且不知道使用哪个函数来连接数据框在一起。我有一个名为 Temp 的数据框它包含 11 年期间每一天的所有 4017 行数据,但其余数据框缺少某些日期。

import pandas as pd

#just creating some sample data to make it easier

Temp = pd.DataFrame({'year':[2009,2009,2009,2010,2010,2010,2011,2011,2011,2012,2012,2012,2013,2013,2013,
2014,2014,2014,2015,2015,2015],'month':[1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3],
'day':[1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3],
'T1':[20,21,25,28,30,33,39,35,34,34,31,30,27,24,20,21,25,28,30,33,39],
'T2':[33,39,35,34,34,31,30,27,24,20,21,25,28,30,33,39,20,21,25,28,30]})

WS = pd.DataFrame({'year':[2009,2009,2010,2011,2011,2011,2012,2012,2012,2013,2013,2013,
2014,2014,2014,2015,2015,2015],'month':[1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3],
'day':[1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3],
'WS1':[5.4,5.1,5.2,4.3,4.4,4.4,1.2,1.5,1.6,2.3,2.5,3.1,2.5,4.6,4.4,4.4,1.2,1.5],
'WS2':[5.4,5.1,4.4,4.4,1.2,1.5,1.6,2.3,2.5,5.2,4.3,4.4,4.4,1.2,1.5,1.6,2.3,2.5]})

RH = pd.DataFrame({'year':[2009,2009,2010,2011,2011,2011,2012,2012,2012,2013,2013,2013,
2014,2014,2014],'month':[1,2,3,1,2,3,1,2,3,1,2,3,1,2,3],
'day':[1,2,3,1,2,3,1,2,3,1,2,3,1,2,3],
'RH1':[33,38,30,45,52,60,61,66,60,59,30,45,52,60,61],
'RH2':[33,38,59,30,45,52,60,61,30,45,52,60,61,66,60]})
好的,到目前为止,我所尝试的是首先创建一个循环,将年、月和日列转换为 DateTime 索引并删除剩余的年、月和日列。
df = [Temp, WS, RH]

for dfs in df:
dfs['date'] = pd.to_datetime(dfs[['year','month','day']])
dfs.set_index(['date'],inplace=True)
dfs.drop(columns = ['year','month','day'],inplace=True)
但我不断收到错误信息,说 TypeError: tuple indices must be integers or slices, not listTypeError: list indices must be integers or slices, not list .由于我无法解决这个问题,因此我无法辨别之后要做什么才能将所有数据帧合并在一起。我假设我必须设置一个索引,如 idx = pd.date_range('2018-01-01 00:00:00', '2018-12-31 23:00:00', freq='H')然后为缺少数据的数据帧重置索引。然后,我不能使用左连接或连接,因为它们都有相同的索引吗? 上面给出的数据框示例没有所需的日期范围,我只是不知道如何制作示例数据框。

最佳答案

这是你要找的吗?

dfs = [Temp, WS, RH]

data = []
for df in dfs:
data.append(df.set_index(pd.to_datetime(df[["year", "month", "day"]]))
.drop(columns=["year", "month", "day"]))
out = pd.concat(data, axis="columns")
>>> out
T1 T2 WS1 WS2 RH1 RH2
2009-01-01 20 33 5.4 5.4 33.0 33.0
2009-02-02 21 39 5.1 5.1 38.0 38.0
2009-03-03 25 35 NaN NaN NaN NaN
2010-01-01 28 34 NaN NaN NaN NaN
2010-02-02 30 34 NaN NaN NaN NaN
2010-03-03 33 31 5.2 4.4 30.0 59.0
2011-01-01 39 30 4.3 4.4 45.0 30.0
2011-02-02 35 27 4.4 1.2 52.0 45.0
2011-03-03 34 24 4.4 1.5 60.0 52.0
2012-01-01 34 20 1.2 1.6 61.0 60.0
2012-02-02 31 21 1.5 2.3 66.0 61.0
2012-03-03 30 25 1.6 2.5 60.0 30.0
2013-01-01 27 28 2.3 5.2 59.0 45.0
2013-02-02 24 30 2.5 4.3 30.0 52.0
2013-03-03 20 33 3.1 4.4 45.0 60.0
2014-01-01 21 39 2.5 4.4 52.0 61.0
2014-02-02 25 20 4.6 1.2 60.0 66.0
2014-03-03 28 21 4.4 1.5 61.0 60.0
2015-01-01 30 25 4.4 1.6 NaN NaN
2015-02-02 33 28 1.2 2.3 NaN NaN
2015-03-03 39 30 1.5 2.5 NaN NaN

关于python - 循环遍历多个数据帧并创建日期时间索引然后加入数据帧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67611811/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com