Transform a Counter object into a Pandas DataFrame
You can construct using from_dict
and pass param orient='index'
, then call reset_index
so you get a 2 column df:
In [40]:from collections import Counterd = Counter({'fb_view_listing': 76, 'fb_homescreen': 63, 'rt_view_listing': 50, 'rt_home_start_app': 46, 'fb_view_wishlist': 39, 'fb_view_product': 37, 'fb_search': 29, 'rt_view_product': 23, 'fb_view_cart': 22, 'rt_search': 12, 'rt_view_cart': 12, 'add_to_cart': 2, 'create_campaign': 1, 'fb_connect': 1, 'sale': 1, 'guest_sale': 1, 'remove_from_cart': 1, 'rt_transaction_confirmation': 1, 'login': 1})df = pd.DataFrame.from_dict(d, orient='index').reset_index()dfOut[40]: index 00 login 11 rt_transaction_confirmation 12 fb_view_cart 223 fb_connect 14 rt_view_product 235 fb_search 296 sale 17 fb_view_listing 768 add_to_cart 29 rt_view_cart 1210 fb_homescreen 6311 fb_view_product 3712 rt_home_start_app 4613 fb_view_wishlist 3914 create_campaign 115 rt_search 1216 guest_sale 117 remove_from_cart 118 rt_view_listing 50
You can rename the columns to something more meaningful:
In [43]:df = df.rename(columns={'index':'event', 0:'count'})dfOut[43]: event count0 login 11 rt_transaction_confirmation 12 fb_view_cart 223 fb_connect 14 rt_view_product 235 fb_search 296 sale 17 fb_view_listing 768 add_to_cart 29 rt_view_cart 1210 fb_homescreen 6311 fb_view_product 3712 rt_home_start_app 4613 fb_view_wishlist 3914 create_campaign 115 rt_search 1216 guest_sale 117 remove_from_cart 118 rt_view_listing 50
Another option is to use DataFrame.from_records
method
import pandas as pdfrom collections import Counterc = Counter({'fb_view_listing': 76, 'fb_homescreen': 63, 'rt_view_listing': 50, 'rt_home_start_app': 46, 'fb_view_wishlist': 39, 'fb_view_product': 37, 'fb_search': 29, 'rt_view_product': 23, 'fb_view_cart': 22, 'rt_search': 12, 'rt_view_cart': 12, 'add_to_cart': 2, 'create_campaign': 1, 'fb_connect': 1, 'sale': 1, 'guest_sale': 1, 'remove_from_cart': 1, 'rt_transaction_confirmation': 1, 'login': 1})df = pd.DataFrame.from_records(list(dict(c).items()), columns=['page','count'])
It's a one-liner and speed seems to be the same.
Or use this variant to have them sorted by most used. Again the performance is about the same.
df = pd.DataFrame.from_records(c.most_common(), columns=['page','count'])
If you want two columns, set the keyword argument orient='index'
when creating a DataFrame
from a dictionary using from_dict
:
final_df = pd.DataFrame.from_dict(final, orient='index')