Source Code:
(back to article)
import pandas as pd # Create a sample DataFrame df = pd.DataFrame({'A': ['foo', 'bar', 'foo', 'bar', 'foo', 'bar', 'foo', 'foo'], 'B': ['one', 'one', 'two', 'three', 'two', 'two', 'one', 'three'], 'C': [1, 2, 3, 4, 5, 6, 7, 8], 'D': [10, 20, 30, 40, 50, 60, 70, 80]}) # Group the DataFrame by columns 'A' and 'B' grouped = df.groupby(['A', 'B']).size().reset_index(name='Counts') # Print the resulting DataFrame print(grouped)
Result:
Report an issue