Home > Back-end >  create and fill a dataframe with dates as index and fill with arrys in pandas
create and fill a dataframe with dates as index and fill with arrys in pandas

Time:10-01

I would like to create a dataframe with dates as index and to fill the dataframe with two array. I try to make myself clear.

I have two array:

import numpy as np
qq = np.zeros(24)
hh = np.zeros(24) 

They refer to each hour of a specific day. I would like to put the in a dataframe. This is what I have tried:

delta = pd.Timedelta("1 days")
start = '2018-01-01 00:00:00'
start_date = pd.to_datetime(start,format='%Y-%m-%d %H:%M:%S')


df = pd.DataFrame([qq,hh] ,columns=['Qout','h'],
                  index=pd.date_range(start_date, periods=24, freq='H'))

This is my solution:

df['Qout'] = qq
df['h']    = hh

What do you think?

Thanks

CodePudding user response:

This is what i came up with, this should give you the desired output. Keep in mind that i am transposing as the dataframe is created. I'm unsure what kind of error you were getting, but i got one where the array's were being read as collumns and as such i transposed the dataframe.

import numpy as np
qq = np.zeros(24)
hh = np.zeros(24)
df = pd.DataFrame([qq,hh,pd.date_range(start_date, periods=24, freq='H')]).T
df.columns = ['Qout','h','Dates']
df = df.set_index('Dates')

CodePudding user response:

If you want 0 in all of your DataFrame you can use this instead

start = '2018-01-01 00:00:00'
start_date = pd.to_datetime(start,format='%Y-%m-%d %H:%M:%S')
df = pd.DataFrame(0,index=pd.date_range(start_date, periods=24, freq='H'),columns = ['Qout','h'])
  • Related