lserlohn lserlohn - 6 months ago 25
Python Question

how to create all zero dataframe in Python

I want to create a dataframe in Python with 24 columns (indicating 24 hours), which looks like this:

column name 0 1 2 3 ... 24

row 1 0 0 0 0 0
row 2 0 0 0 0 0
row 3 0 0 0 0 0


I would like to know how to initialize it? and in the future, I may add row 4, with all "0", how to do that? Thanks,

Answer

You can create an empty numpy array, convert it to a dataframe, and then add the header names.

 import numpy
 a = numpy.zeros(shape=(3,24))
 df = pd.DataFrame(a,columns=['col1','col2', etc..])

to set row names use

 df.set_index(['row1', 'row2', etc..])

if you must.