1# Basic syntax:
2# Get counts of duplicated elements in one column:
3dataframe.pivot_table(index=['column_name'], aggfunc='size')
4# Get counts of duplicated elements across multiple columns:
5dataframe.pivot_table(index=['column_1', 'column_2',...], aggfunc='size')
6
7# Note, the column (column_name) doesn't need to be sorted
8# Note, this will return a Series object containing column_name and
9# a column with the number of occurrences of each value in column_name
10
11# One approach to adding the counts back to the original dataframe:
12counts = dataframe.pivot_table(index=['column_name'], aggfunc='size')
13counts = pd.DataFrame(counts) # Convert Series to DataFrame
14counts.index.name = 'column_name'
15counts.reset_index(inplace=True) # Change row names to be a column
16counts.columns = ['column_name', 'counts']
17dataframe = dataframe.merge(counts) # Merge dataframes on common column