【问题标题】:Copy row to another dataframe将行复制到另一个数据框
【发布时间】:2020-07-01 09:50:12
【问题描述】:

我有 2 个索引类型的数据框:Datatimeindex,我想将一行复制到另一行。数据框是:

变量:df

DateTime
2013-01-01 01:00:00    0.0
2013-01-01 02:00:00    0.0
2013-01-01 03:00:00    0.0
....
Freq: H, Length: 8759, dtype: float64

变量:consumer_year

Potência Ativa  ...    Costs
Datetime                             ...         
2019-01-01 00:00:00       11.500000  ...  1.08874
2019-01-01 01:00:00        6.500000  ...  0.52016
2019-01-01 02:00:00        5.250000  ...  0.38183
2019-01-01 03:00:00        5.250000  ...  0.38183

[8760 rows x 5 columns]

这是我的代码:

mc.run_model(tmy_data)

df=round(mc.ac.fillna(0)/1000,3)

consumption_year['PVProduction'] = df.iloc[:,[1]] #1
consumption_year['PVProduction'] = df[:,1] #2

我正在尝试将 df 的第二列复制到 consumer_year 数据框中的新列,但这些以前的经验都没有奏效。查看索引,我发现了 3 个主要区别:

  1. 年(2013 年和 2019 年)
  2. 开始时间:01:00 和 00:00
  3. 长度:8760 和 8759

在将一行复制到另一行之前,我是否需要先解决这 3 个差异(使 df 的日期时间等于 consumer_year)?如果是这样,您能否为我提供解决这些差异的解决方案。

这些是错误:

1: consumption_year['PVProduction'] = df.iloc[:,[1]] 
 raise IndexingError("Too many indexers")
pandas.core.indexing.IndexingError: Too many indexers

2: consumption_year['PVProduction'] = df[:,1]
 raise ValueError("Can only tuple-index with a MultiIndex")
ValueError: Can only tuple-index with a MultiIndex

【问题讨论】:

    标签: python dataframe datetime pvlib


    【解决方案1】:

    您可以将两个数据框合并在一起。

    pd.merge(df, consumption_year, left_index=True, right_index=True, how='outer')  
    

    【讨论】:

    • 我得到了一个 [17519 行 x 6 列] 的数据框,即 2 年(2013 年和 2019 年)。我想要一个只有 1 年(8760)的最终数据框和所有的消费年列和 1 列的 df。 (无论指数,2013还是2019
    • 你必须选择年份 df.loc['2013'] 你也可以dropna或者使用inner join。
    猜你喜欢
    • 1970-01-01
    • 2019-10-20
    • 2019-09-28
    • 2018-03-29
    • 1970-01-01
    • 2023-03-16
    • 1970-01-01
    • 2018-08-03
    • 1970-01-01
    相关资源
    最近更新 更多