Python Forum

Full Version: make a frequency table
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi, I'd like to make a table showing the frequency with which numbers occur each month.
My input is a csv file with a row format of:

Date(d/m/y) num1 num2 num3 num4 num5 num6

for the output the horizontal axes is the months 1 - 12, the vertical axes is numbers 1 - 37.
Each intersection (row/column) will contain the total occurrences for that number that month.
Much appreciated if someone can point me in the right direction.
Thanks
David
as a bare minimum if you want to stick with standard library:
csv module to read the file
collections.defaultdict - to collect data per month
collections.Counter to count occurrences
datetime - to parse date

or you can use pandas

https://www.google.com/search?q=python+frequency+table
Thanks for this Buran, I already have a csv file going back a few years, so presumably I can forget collections.defaultdict.
(Dec-20-2018, 01:30 PM)davidm Wrote: [ -> ]I already have a csv file going back a few years, so presumably I can forget collections.defaultdict
No, my understanding is each day is a row. So, you will need to parse the file and create some data structure that will store all the numbers for given month (and year?). That's where the defaultdict comes handy.

Again, this is if you are not going to use pandas
Hi Barun, been struggling with this, having trouble getting my head round Python.
Maybe you can give me a little more help.
Here's an example of the csv input file:
Str, N1, N2, N3, N4, N5, N6
Sag, 15, 21, 23, 27, 36, 37
Sag, 8, 11, 13, 18, 25, 31
Sag, 1, 3, 7, 9, 15, 22
Sag, 7, 8, 25, 31, 32, 35
Sag, 3, 5, 8, 18, 19, 29
Sag, 2, 12, 14, 18, 22, 25
Sco, 3, 5, 16, 18, 21, 26
Sco, 6, 16, 23, 24, 30, 33
Sco, 9, 20, 25, 27, 34, 37
Sco, 16, 18, 20, 22, 24, 25
Sco, 8, 9, 13, 14, 27, 29
Sco, 2, 7, 9, 19, 24, 25
Sco, 4, 8, 13, 20, 27, 29
Sco, 6, 7, 11, 17, 22, 29
Sco, 11, 17, 19, 25, 27, 28
Sco, 4, 16, 23, 29, 35, 36
Lib, 7, 20, 21, 25, 35, 37
Lib, 8, 13, 14, 27, 30, 37
Lib, 1, 5, 15, 17, 23, 37
Lib, 6, 16, 21, 27, 28, 29

The 1st column - Str - has 12 possible values, I want 12 columns - for each Str value. The cells in the table contain integers 1-37 in 6 cols - N1 - N6 - I need to create a table with the 12 columns and 37 rows - 1 for each of the 37 integers in the cells, and each cell should contain a count of the integers for that row/column intersect.
So for the above input, output for column:
Sag
1. 1
2. 1
3. 2
4. 0
5. 1
6. 0
7. 2
8. 3
.
.
.
37. 1

Really appreciate any help
Thanks
David
Have you simply tried to READ the file with python. Here is how!
My problem is getting a count of the values.
Thanks