Group by date and count of categorical variable date wise

Date frame having two categorical variable column with date time stamp.

Date Time Va Vb
01-01-2023 05:55 A B
01-01-2023 06:25 A
01-01-2023 17:42 B
01-01-2023 19:17 A B
02-01-2023 05:55 A B
02-01-2023 06:25 A B
02-01-2023 17:42 A B
02-01-2023 19:17 A

To group by the set by date and count Va and Vb for a date. Expected Result:

Va Vb
01-01-2023 3 3
02-01-2023 4 3

Wrote in previous slide

Upvotes: 1

Views: 37

Answers (2)

Andrej Kesely
Andrej Kesely

Reputation: 195573

Try:

out = df[['Date', 'Va', 'Vb']].groupby('Date').count()
print(out)

Prints:

            Va  Vb
Date              
01-01-2023   3   3
02-01-2023   4   3

Upvotes: 0

Cetin Basoz
Cetin Basoz

Reputation: 23837

If you are using an SQL database (and those empty values are NULL):

Select Date, Count(Va) as Va, Count(Vb) as Vb 
  from sourceTable 
  group by date;

DBFiddle demo

Upvotes: 0

Related Questions