["python","pandas","group-by","dataframe"]
Get a frequency count based on multiple dataframe columns

I have the following dataframe. Group Size Short Small Short Small Moderate Medium Moderate Small Tall Large I want to count the frequency of how many times the same row appears in the dataframe. Group Size Time Short Small 2 Moderate Medium 1 Moderate Small 1 Tall Large 1
You can use groupby's size import pandas as pd # load the sample data data = {'Group': ['Short', 'Short', 'Moderate', 'Moderate', 'Tall'], 'Size': ['Small', 'Small', 'Medium', 'Small', 'Large']} df = pd.DataFrame(data) Option 1: dfg = df.groupby(by=["Group", "Size"]).size() # which results in a pandas.core.series.Series Group Size Moderate Medium 1 Small 1 Short Small 2 Tall Large 1 dtype: int64 Option 2: dfg = df.groupby(by=["Group", "Size"]).size().reset_index(name="Time") # which results in a pandas.core.frame.DataFrame Group Size Time 0 Moderate Medium 1 1 Moderate Small 1 2 Short Small 2 3 Tall Large 1 Option 3: dfg = df.groupby(by=["Group", "Size"], as_index=False).size() # which results in a pandas.core.frame.DataFrame Group Size Time 0 Moderate Medium 1 1 Moderate Small 1 2 Short Small 2 3 Tall Large 1