MJS MJS - 1 month ago 8
Python Question

pls help me create a simple dataframe with row numbers in each column

How can I generate this dataframe on a much larger scale without typing in lists?

df = pd.DataFrame([[0,0,0,0,0],[1,1,1,1,1],[2,2,2,2,2],[3,3,3,3,3],[4,4,4,4,4]], \
index=pd.date_range(start='2000-01-01',periods=5),columns=['a','b','c','d','e'])


Thanks!

Answer

You can use np.repeat with reshape to get the final df you desire, this will be more efficient and less typing than your current solution:

In [17]:
df = pd.DataFrame(pd.Series(np.repeat(np.arange(5),5)).reshape(5,-1), columns=list('abcde'), index=pd.date_range(start='2000-01-01',periods=5))
df

Out[17]:
            a  b  c  d  e
2000-01-01  0  0  0  0  0
2000-01-02  1  1  1  1  1
2000-01-03  2  2  2  2  2
2000-01-04  3  3  3  3  3
2000-01-05  4  4  4  4  4