Python Forum
Randow DataFrame - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Data Science (https://python-forum.io/forum-44.html)
+--- Thread: Randow DataFrame (/thread-7997.html)



Randow DataFrame - ArnaudCoulon - Feb-02-2018

Hey guys !

I was trying to make a 4*54 random numbers DataFrame. I spent 1 hour trying to figure out how to do it and I wrote this code :
import pandas as pd
from random import randint
df = pd.DataFrame()
for i in range(53):
    data1 = pd.DataFrame([randint(1,3)])
    data2 = pd.DataFrame([randint(1,3)])
    data3 = pd.DataFrame([randint(1,3)])
    data4 = pd.DataFrame([randint(1,3)])
    data = pd.DataFrame(pd.concat([data1, data2, data3, data4], axis =1, join_axes=[data1.index]))

    df = df.append(data, ignore_index = True)
I'm satisfied with the result but I think there should be a better way to do it.

What do you think about it ? :)

Have a nice day all !


RE: Randow DataFrame - ka06059 - Feb-03-2018

just in case u have 100+ (data1,data2,data3....) datas for each loop u can create nested lists without having to label variables manually.

for i in range(53):
  datas = []
  for numdata in range(3):  #4 items created in each datas variable 
      datas.append(pd.DataFrame([randint(1,3)]))
  data = pd.DataFrame(pd.concat(datas, axis =1, join_axes=[datas[0].index])) # datas[0] is data1
  df = df.append(datas, ignore_index = True)