Random Numbers in Excel into percentage

Question:

Hi my code works fine but is there any way to print how many times numbers 1-6 were said into a percentage

I haven’t tried anything yet.

import pandas as pd
import random

data = [random.randint(0,6) for _ in range(10)]

df = pd.DataFrame(data)

print(df)

df.to_excel(r'H:Grade10CsMir Hussain 12.00.00 3.xlsx', index=False)
Asked By: HussainMir

||

Answers:

So from your data you could do:

import random
from collections import Counter


data = [random.randint(0,6) for _ in range(10)]
total_data = [data]

frequency = Counter(data)
number_elements = len(data)

total_data.append(list((frequency[item] / number_elements)*100 if item != 0 else '' for item in total_data[0]))

df = pd.DataFrame(total_data)

print(df)

df.to_excel(r'H:Grade10CsMir Hussain 12.00.00 3.xlsx', index=False)

Item frequency count in Python

Answered By: Andrew Ryan
import pandas as pd
import random

data = [random.randint(0,6) for _ in range(10)]

df = pd.DataFrame(data)

df = df.values_count(normalize=True) # this line makes the percentages

df.to_excel(r'H:Grade10CsMir Hussain 12.00.00 3.xlsx', index=False)
Answered By: L F
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.