How do I turn dataframe#1 into something i can properly graph, like dataframe#2?

I have the following dataframe, based on data i pulled from my database:

date event_type count
2022-05-10 page_view 3
2022-05-11 cart_add 2
2022-05-11 page_view 2
2022-05-12 cart_add 1
2022-05-12 cart_remove 1
2022-05-12 page_view 2
2022-05-13 cart_remove 2
2022-05-13 page_view 1
2022-05-14 cart_add 2
2022-05-14 page_view 5

Basically I am tracking 3 things on my website:

  1. when a user views a product page
  2. when a user adds a product to their cart
  3. when a user removes a product from their cart

I’m tracking how often each of these events happens in a day and I want to then graph them all on a single line chart. In order to do that, I think I need to make it look something more like this:

date page_views cart_adds cart_removes
2022-05-10 3 0 0
2022-05-11 2 2 0
2022-05-12 2 1 1
2022-05-13 1 0 2
2022-05-14 5 2 0

I am very new to pandas and not even sure if this library is what I should be using. So forgive my cluelessness, but how do I make dataframe1 look like dataframe2?

>Solution :

df.pivot(columns='event_type', index='date').fillna(0)

Output:

              count                      
event_type cart_add cart_remove page_view
date                                     
2022-05-10      0.0         0.0       3.0
2022-05-11      2.0         0.0       2.0
2022-05-12      1.0         1.0       2.0
2022-05-13      0.0         2.0       1.0
2022-05-14      2.0         0.0       5.0

Leave a Reply