-1

I don't know how i pivot table in picture one to square matrix in table 2.

Value is distinct count anonymous_id

Want to know how many users join in event voucher denied and item_checkout_started, etc. ?

Thanks!

enter image description here

enter image description here

SeaBean
  • 22,547
  • 3
  • 13
  • 25
Mello
  • 1
  • 2
  • Could U provide df? – Piotr Żak Oct 09 '21 at 15:55
  • Does this answer your question? [How can I pivot a dataframe?](https://stackoverflow.com/questions/47152691/how-can-i-pivot-a-dataframe) – mozway Oct 09 '21 at 15:55
  • https://drive.google.com/file/d/1CjcsAszvDqzcP1BMdGyQyZVwMVDKOhXu/view?usp=sharing This my csv file. – Mello Oct 09 '21 at 16:11
  • @mozway I dont find my answer in your link attach "How can i pivot dataframe" look my picture 2, i want to dataframe like table in picture 2, You can mean How much user join in event voucher denied and item_checkout_started? – Mello Oct 09 '21 at 16:53

2 Answers2

1

pivot require 3 argumnts:

  • index (anonymous_id) - in this case
  • column - Column to use to make new frame’s columns.
  • values - Column(s) to use for populating new frame’s values.

df.pivot(index=["anonymous_id"], columns=["list_event_n"],values="at")
Barbaros Özhan
  • 59,113
  • 10
  • 31
  • 55
Piotr Żak
  • 2,046
  • 5
  • 18
  • 30
  • It not my output. i want output lịke picture 2, its square martrix. You can mean How much user join in event voucher denied and item_checkout_started? – Mello Oct 09 '21 at 16:40
0

You can try .pivot_table() with aggfunc='count' to count the number of occurrences of anonymous_id, as folllows:

df.pivot_table(index='list_event_n', columns='list_event_n', values='anonymous_id', aggfunc='count')

Result:

list_event_n            item_checkout_started  item_viewed  outlet_list_viewed  outlet_product_clicked  outlet_promo_clicked  popup_registered  popup_viewed  product_clicked  product_list_viewed  product_searched  product_viewed  promotion_viewed  item_checkout_started  item_viewed  outlet_list_viewed  outlet_product_clicked  outlet_promo_clicked  popup_registered  popup_viewed  product_clicked  product_list_viewed  product_searched  product_viewed  promotion_viewed
list_event_n                                                                                                                                                                                                                                                                                                                                                                                                                                                                          
item_checkout_started                     1.0          NaN                 NaN                     NaN                   NaN               NaN           NaN              NaN                  NaN               NaN             NaN               NaN                    1.0          NaN                 NaN                     NaN                   NaN               NaN           NaN              NaN                  NaN               NaN             NaN               NaN
item_viewed                               NaN         15.0                 NaN                     NaN                   NaN               NaN           NaN              NaN                  NaN               NaN             NaN               NaN                    NaN         15.0                 NaN                     NaN                   NaN               NaN           NaN              NaN                  NaN               NaN             NaN               NaN
outlet_list_viewed                        NaN          NaN                 2.0                     NaN                   NaN               NaN           NaN              NaN                  NaN               NaN             NaN               NaN                    NaN          NaN                 2.0                     NaN                   NaN               NaN           NaN              NaN                  NaN               NaN             NaN               NaN
outlet_product_clicked                    NaN          NaN                 NaN                     1.0                   NaN               NaN           NaN              NaN                  NaN               NaN             NaN               NaN                    NaN          NaN                 NaN                     1.0                   NaN               NaN           NaN              NaN                  NaN               NaN             NaN               NaN
outlet_promo_clicked                      NaN          NaN                 NaN                     NaN                   7.0               NaN           NaN              NaN                  NaN               NaN             NaN               NaN                    NaN          NaN                 NaN                     NaN                   7.0               NaN           NaN              NaN                  NaN               NaN             NaN               NaN
popup_registered                          NaN          NaN                 NaN                     NaN                   NaN               1.0           NaN              NaN                  NaN               NaN             NaN               NaN                    NaN          NaN                 NaN                     NaN                   NaN               1.0           NaN              NaN                  NaN               NaN             NaN               NaN
popup_viewed                              NaN          NaN                 NaN                     NaN                   NaN               NaN           1.0              NaN                  NaN               NaN             NaN               NaN                    NaN          NaN                 NaN                     NaN                   NaN               NaN           1.0              NaN                  NaN               NaN             NaN               NaN
product_clicked                           NaN          NaN                 NaN                     NaN                   NaN               NaN           NaN             17.0                  NaN               NaN             NaN               NaN                    NaN          NaN                 NaN                     NaN                   NaN               NaN           NaN             17.0                  NaN               NaN             NaN               NaN
product_list_viewed                       NaN          NaN                 NaN                     NaN                   NaN               NaN           NaN              NaN                  7.0               NaN             NaN               NaN                    NaN          NaN                 NaN                     NaN                   NaN               NaN           NaN              NaN                  7.0               NaN             NaN               NaN
product_searched                          NaN          NaN                 NaN                     NaN                   NaN               NaN           NaN              NaN                  NaN               2.0             NaN               NaN                    NaN          NaN                 NaN                     NaN                   NaN               NaN           NaN              NaN                  NaN               2.0             NaN               NaN
product_viewed                            NaN          NaN                 NaN                     NaN                   NaN               NaN           NaN              NaN                  NaN               NaN            16.0               NaN                    NaN          NaN                 NaN                     NaN                   NaN               NaN           NaN              NaN                  NaN               NaN            16.0               NaN
promotion_viewed                          NaN          NaN                 NaN                     NaN                   NaN               NaN           NaN              NaN                  NaN               NaN             NaN               2.0                    NaN          NaN                 NaN                     NaN                   NaN               NaN           NaN              NaN                  NaN               NaN             NaN               2.0
SeaBean
  • 22,547
  • 3
  • 13
  • 25
  • next step can you convert ? i want my index = df.column, columns = df.columns, value = distinct count(anonymous_id). – Mello Oct 10 '21 at 01:54
  • 1
    @Mello What to convert? Can you advise what's the outstanding task to meet your goal? Remaining to make index the same as column index? But then how to display the anonymous_id ? – SeaBean Oct 10 '21 at 02:05
  • how many anonymous_id participated in both events?, i need count column anonymous_id, but not anonymous_id is index. You look my picture 2, dont care column and row anonymous_id because i want to count anonymous_id and fill in table 2, and i have no idea for them. @SeaBean – Mello Oct 10 '21 at 03:13
  • sorry i am beginer stackoverflow, you can see my ideas below – Mello Oct 10 '21 at 03:50
  • @Mello See my edit above. – SeaBean Oct 10 '21 at 08:46
  • Your result almost correct. But, you look value (item_viewed ; item_checkout_started) = NaN, i need that exact value instead of Nan. – Mello Oct 10 '21 at 14:57
  • Help me please, that my stuck from thurdays to today :( – Mello Oct 10 '21 at 15:11
  • @Mello With further thoughts, your requirement is more complicated than it looks. You need combinations of any 2 itesms e.g. if an id has 3 events: `item_checkout_started`, `item_viewed` and `outlet_list_viewed`, you need the 3 paried combinations of (event 1, event 2), (event 1, event 3) and also (event 2, event 3) each to count this id. Frankly speaking, I currently have no idea how to support it. Sorry for that. – SeaBean Oct 10 '21 at 15:39
  • thank you, i sure post my answer when i find solution for. In the easy but difficult job problem. – Mello Oct 11 '21 at 02:01