I'm currently creating a Python 3 program that will pick out the most frequent numbers from a six-column CSV. So far I have the code that will pick the most frequent from each column, but I also want code that can pick the six most frequent (from the 1st most frequent, to the 6th) number from all the columns and rows combined.
I have an Excel spreadsheet that does it, using the formula:
=MODE(IF(1 - ISNUMBER(MATCH(B2:G402,$L$24:L24,0)),B2:G402))
Then dragging the calculation down to display six numbers (as far as I can tell this is a working formula!)
Is there a way I can get that formula, or something better, in Python 3? So the code will display the top six most frequent numbers from six columns and 400+ rows?
Here's my code so far:
import csv
import os
import random
from collections import Counter
filename='lotto.csv'
os.system('cls' if os.name == 'nt' else 'clear')
print("\n\n********** Lottery Number Generator **********\n\n")
print("Based on all previous Lotto numbers from CSV.\n")
x = 1
while x < 8:
with open(filename, 'r') as f:
column = (row[x] for row in csv.reader(f))
print("Lotto Number", x, ": {0}".format(Counter(column).most_common()[0][0]))
x = x + 1
Any ideas, guys?
Thanks in advance!
Dave
Here is for me the simplest way for you.
I will be using pandas (install it with pip install pandas
) :
import pandas as pd
df = pd.read_csv('filename.csv')
freq = df.stack().value_counts()
It will get you a list with the frequencies of each element in the array.
However, if you want the frequency of only one column, you can do this :
freq = df['column_name'].value_counts()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.