Generating Pivot Tables in Python - Pandas? Numpy? Xlrd? from csv
Question: I have been searching for hours, literally the entire day on how to generate a
pivot table in Python. I am very new to python so please bear with me.

What I want is to take a csv file, extract the first column and generate a
pivot table using the count or frequency of the numbers in that column, and
sort descending

    
    
    import pandas
    
    import numpy 
    
    
    from numpy import recfromtxt
    a = recfromtxt('1.csv', skiprows=1, usecols=0, delimiter=',')
    
    
    print a
    

^ what i get here is a list of the first column [2 2 2 6 7]

What i need is an export of 2 columns

2--3

6--1

7--1


Answer: Have you had a look here?

<https://pypi.python.org/pypi/pivottable>

Otherwise, from you example, you might just use list comprehensions:

    
    
    >>> l = [2,2,2,6,7]
    >>> [(i, l.count(i)) for i in set(l)]
    
    [
        (2,3),
        (6,1),
        (7,1)
    ]
    

Or even dictionary comprehensions, depending on what you need:

    
    
    >>> l = [2,2,2,6,7]
    >>> {i:l.count(i) for i in set(l)}
    
    {
        2: 3,
        6: 1,
        7: 1
    }
    

**edit** (suggestions from @Peter DeGlopper)

Another more efficient way using
[collections.Counter](http://docs.python.org/2/library/collections.html#counter-
objects) (read comments below):

    
    
    >>> from collections import Counter
    >>> l = [2,2,2,6,7]
    >>> Counter(l)
    
    Counter({2: 3, 6: 1, 7: 1})
    

