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
collections.Counterclass makes this very easy if you have access to it (2.7 or later) and don't specifically need your count array to be a numpy array. You can generate one fromcollections.Counter(np.nditer(a)). If you neednumpyoutput and your data is nonnegative integers, it looks likebincountwould be a start: docs.scipy.org/doc/numpy/reference/generated/…unq, _ = np.unique(a, reverse_index=True); cnts = np.bincount(_), and nowunqandcntsare your two columns.return_inverserather thanreverse_index, right? And you might needreturn_indexas well to make it easier to match the counts up against the array entries. Still, that's clever.return_inverse... I have written somewhere around here that that will eventually become a standard feature ofnp.bincount, because I find myself writing those do lines of code much too often.