Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Randow DataFrame
#1
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 !
Reply
#2
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)
swallow osama bin laden
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020