How to performe count like this query in pandas?
Select col1, col2, count(col3) as total from table
GROUP by col1,col2
How to performe count like this query in pandas?
Select col1, col2, count(col3) as total from table
GROUP by col1,col2
You want the pandas groupby method:
df2 = df.groupby(['col1', 'col2'], as_index = False).count()
This will give you a count of all your other columns. If you want to specify a different aggregation function for each column, you can use .agg
:
df2 = df.groupby(['col1', 'col2'], as_index = False).agg({'col3': 'count', 'col4': 'sum'})