0

I have 2 dataframes with index type: Datatimeindex and I would like to copy one row to another. The dataframes are:

variable: 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

variable: consumption_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]

here is my code:

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

I am trying to copy the second column of df, to a new column in consumption_year dataframe but none of those previous experiences worked. Looking to the index, I see 3 major differences:

  1. year (2013 and 2019)
  2. starting hour: 01:00 and 00:00
  3. length: 8760 and 8759

Do I need to solve those 3 differences first (making an datetime from df equal to consumption_year), before I can copy one row to another? If so, could you provide me a solution to fix those differences.

Those are the errors:

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
PMendes
  • 29
  • 4

1 Answers1

0

You can merge two data frames together.

pd.merge(df, consumption_year, left_index=True, right_index=True, how='outer')  
Pramote Kuacharoen
  • 1,496
  • 1
  • 5
  • 6
  • I got a dataframe with [17519 rows x 6 columns], i.e, 2 years (2013 and 2019). I would like to have a final dataframe only with 1 year (8760) and all columns of consumption_year and 1 column of df. (no matter the index, 2013 or 2019 – PMendes Jul 01 '20 at 11:13
  • 1
    You have to select the year df.loc['2013'] You can also dropna or use inner join. – Pramote Kuacharoen Jul 01 '20 at 22:42