Search code examples
pythonpandasdataframerollup

Rolling up data frame along with count of rows in python


I am still in a learning phase in python and wanted to know how do we roll up the data and count the duplicate data rows in a column called count

The data frame structure is as follows

Col1| Value
A   |  1
B   |  1
A   |  1
B   |  1
C   |  3
C   |  3
C   |  3
C   |  3

My result should be as follows

Col1|Value|Count
 A  |  1  | 2
 B  |  1  | 2
 C  |  3  | 4

Solution

  • >>> df2 = df.groupby(['Col1', 'Value']).size().reset_index()
    >>> df2.columns = ['Col1', 'Value', 'Count']
    >>> df2
      Col1  Value  Count
    0    A      1      2
    1    B      1      2
    2    C      3      4