Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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,

like image 555
lserlohn Avatar asked Feb 06 '15 00:02

lserlohn


People also ask

How do you create a DataFrame with all zeros?

Use numpy. zeros() to create a zero-filled DataFrame Call pandas. DataFrame(data) with data as numpy. zeros(shape) to create a new DataFrame object of size shape populated with 0.0 .

How do I make an empty DataFrame in Python?

You can create an empty dataframe by importing pandas from the python library. Later, using the pd. DataFrame(), create an empty dataframe without rows and columns as shown in the below example.

What is loc [] in Python?

loc[source] Access a group of rows and columns by label(s) or a boolean array. .loc[] is primarily label based, but may also be used with a boolean array.


1 Answers

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.

like image 122
Andy Hayden Avatar answered Sep 27 '22 16:09

Andy Hayden