[英]Python version of an Excel Formula
I'm currently creating a Python 3 program that will pick out the most frequent numbers from a six-column CSV. 我当前正在创建一个Python 3程序,该程序将从六列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: 我有一个使用以下公式的Excel电子表格:
=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? 有没有办法在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
) : 我将使用pandas(通过
pip install pandas
进行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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.