create a table that counts the number of times each integer from 1 to 100 appears in an array, including a row for the numbers between 1 to 100 that have 0 occurrences in r

main.py
import pandas as pd

# create an example array
arr = [1, 2, 3, 3, 5, 10, 20, 20, 20]

# create a dictionary to count occurrences
count_dict = {}
for i in range(1, 101):
    count_dict[i] = arr.count(i)

# create a DataFrame
df = pd.DataFrame(count_dict.items(), columns=['Number', 'Count'])

# add row for numbers with 0 occurrences
for i in range(1, 101):
    if i not in df['Number'].values:
        df = df.append({'Number': i, 'Count': 0}, ignore_index=True)

# sort the DataFrame by number
df = df.sort_values('Number')

print(df)
536 chars
23 lines

related categories

gistlibby LogSnag