How to group identical values and count their frequency in Python?
Newbie to analytics with Python so please be gentle :-) I couldn't find the answer to this question - apologies if it is already answered elsewhere in a different format.
I have a dataset of transaction data for a retail outlet. Variables along with explanation are:
- section: the section of the store, a str;
- prod_name: name of the product, a str;
- receipt: the number of the invoice, an int;
- cashier, the number of the cashier, an int;
- cost: the cost of the item, a float;
- date, in format MM/DD/YY, a str;
- time, in format HH:MM:SS, a str;
Receipt has the same value for all the products purchased in a single transaction, thus it can be used to determine the average number of purchases made in a single transaction.
What is the best way to go about this? I essentially want to use groupby()
to group the receipt variable by its own identical occurrences so that I can create a histogram.
Working with the data in a pandas DataFrame.
EDIT:
Here is some sample data with header (prod_name is actually a hex number):
section,prod_name,receipt,cashier,cost,date,time
electronics,b46f23e7,102856,5,70.50,05/20/15,9:08:20
womenswear,74558d0d,102857,8,20.00,05/20/15,9:12:46
womenswear,031f36b7,102857,8,30.00,05/20/15,9:12:47
menswear,1d52cd9d,102858,3,65.00,05/20/15,9:08:20
From this sample set I would expect a histogram of receipt that shows two occurrences of receipt 102857 (since that person bought two items in one transaction) and one occurrence respectively of receipt 102856 and of receipt 102858. Note: my dataset is not huge, about 1 million rows.
Topic ipython pandas python statistics
Category Data Science