15

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,

1

2 Answers 2

19

There's a trick here: when DataFrame (or Series) constructor is passed a scalar as the first argument this value is propogated:

In [11]: pd.DataFrame(0, index=np.arange(1, 4), columns=np.arange(24))
Out[11]:
   0   1   2   3   4   5   6   7   8   9  ...  14  15  16  17  18  19  20  21  22  23
1   0   0   0   0   0   0   0   0   0   0 ...   0   0   0   0   0   0   0   0   0   0
2   0   0   0   0   0   0   0   0   0   0 ...   0   0   0   0   0   0   0   0   0   0
3   0   0   0   0   0   0   0   0   0   0 ...   0   0   0   0   0   0   0   0   0   0

[3 rows x 24 columns]

Note: np.arange is numpy's answer to python's range.

Sign up to request clarification or add additional context in comments.

Comments

12

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.

2 Comments

shouldn't a = numpy.zeros(shape(3,24)) ?
@DiegoAgher good point (corrected) The theory still holds though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.