Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

A want to create a sample dataframe -- based on a json template -- that looks as real as possible. Hence normal distribution.

This is what I have tried

import json, random
import pandas as pd

sample_data = """{"product1":[
    {"category":"Fruits",
    "productlist":["Bell Peppers","Red Chillies", "Onions", "Tomatoes"]}
],
"product2":[
    {"category":"Vegetables",
    "productlist":["Apple","Mango","Banana"]}
]}"""

products = json.loads(sample_data)

colHeaders = []

for k,v in products.items():
    colHeaders.append(v[0]['category'])

df = pd.DataFrame(columns= colHeaders)

for i in range (1000):
    itemlist = []
    for k,v in products.items():
        itemlist.append(random.choice(v[0]['productlist']))
    #print(itemlist)
    df.loc[len(df)] = itemlist

print(df)

I am not sure I am doing it correctly. If not, please help me with

  • How to check if the data frame rows represent a normal distribution?
  • How to try other distributions in this case?

Other related Stack Overflow questions I have referred are:

question from:https://stackoverflow.com/questions/65672116/creating-pandas-dataframe-in-normal-distribution

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
53 views
Welcome To Ask or Share your Answers For Others

1 Answer

I think what you should do is generate integers in normal distribution and make them the indices of the list. Also graphing the numbers you generated is in my opinion the best way to check whether they are a normal distribution, it should resemble the normal distribution bell shape. However since 20 is such a small number, it may not exactly be the desired shape which is something to keep in mind. The following link I think has all the information you need.

How to generate a random normal distribution of integers


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...