【问题标题】:loop through multiple dataframes and create datetime index then join dataframes循环遍历多个数据帧并创建日期时间索引,然后加入数据帧
【发布时间】:2021-08-09 04:50:44
【问题描述】:

我有 9 个不同长度但格式相似的数据帧。每个数据框都有一个yearmonthday 列,其日期跨度为1/1/2009-12/31/2019,但有些数据框在某些日子里缺少数据。我想用 DateTime 索引构建一个大型数据框,但是我无法创建一个循环来将年、月和日列转换为每个数据框的日期时间索引,并且不知道要使用哪个函数来加入数据框在一起。我有一个名为 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') 这样的索引,然后为缺少数据的数据帧设置reset_index。然后,我不能使用左连接或连接,因为它们都有相同的索引吗? 上面给出的数据框示例没有所需的日期范围,我只是不知道如何制作示例数据框。

【问题讨论】:

  • 你不需要循环:pd.to_datetime(Temp[["year", "month", "day"]])
  • 是的,但我必须为 9 个数据帧这样做,这似乎很乏味,所以我想用一个 swift 代码来完成。

标签: python pandas loops join datetimeindex


【解决方案1】:

你在找什么?

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

【讨论】:

  • pd.concat 会自动用NaN 填充缺失的数据吗?
  • 是的。来自文档:“将 DataFrame 对象与重叠列组合并返回所有内容。交叉点之外的列将填充 NaN 值。”
  • @Heather。如果你想拥有一个数据框,其中包含你的第一个日期和最后一个日期之间的所有日期,你可以这样做:out.reindex(pd.date_range(out.index.min(), out.index.max(), freq="D"))
猜你喜欢
  • 2018-11-13
  • 1970-01-01
  • 2021-12-25
  • 1970-01-01
  • 1970-01-01
  • 2021-12-22
  • 1970-01-01
  • 1970-01-01
  • 2021-12-15
相关资源
最近更新 更多