Sometimes, you may wish to get a count or tally of the occurrences of particular values in a column. For example, you might have a healthcare dataset and you want to know how many times particular payment methods were used during patient visits. In SQL, you could write a query that uses a GROUP BY clause in conjunction with an aggregate function (in this case, COUNT(*)) to get a tally of the payment methods:
SELECT payment, COUNT(*)
FROM data
GROUP BY payment;
In pandas, the same result is accomplished by chaining together the groupby() and size() functions:
tallies = df3.groupby('new_col4').size()
print(tallies)
The output is as follows:
new_col4 1 3 5 1 7 1 21 1 23 1 dtype: int64