Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python equivalent of R table

I have a list

[[12, 6], [12, 0], [0, 6], [12, 0], [12, 0], [6, 0], [12, 6], [0, 6], [12, 0], [0, 6], [0, 6], [12, 0], [0, 6], [6, 0], [6, 0], [12, 0], [6, 0], [12, 0], [12, 0], [0, 6], [0, 6], [12, 6], [6, 0], [6, 0], [12, 6], [12, 0], [12, 0], [0, 6], [6, 0], [12, 6], [12, 6], [12, 6], [12, 0], [12, 0], [12, 0], [12, 0], [12, 6], [12, 0], [12, 0], [12, 6], [0, 6], [0, 6], [6, 0], [12, 6], [12, 6], [12, 6], [12, 6], [12, 6], [12, 0], [0, 6], [6, 0], [12, 0], [0, 6], [12, 6], [12, 6], [0, 6], [12, 0], [6, 0], [6, 0], [12, 6], [12, 0], [0, 6], [12, 0], [12, 0], [12, 0], [6, 0], [12, 6], [12, 6], [12, 6], [12, 6], [0, 6], [12, 0], [12, 6], [0, 6], [0, 6], [12, 0], [0, 6], [12, 6], [6, 0], [12, 6], [12, 6], [12, 0], [12, 0], [12, 6], [0, 6], [6, 0], [12, 0], [6, 0], [12, 0], [12, 0], [12, 6], [12, 0], [6, 0], [12, 6], [6, 0], [12, 0], [6, 0], [12, 0], [6, 0], [6, 0]] 

I want to count the frequency of each element in this list. Something like

freq[[12,6]] = 40 

In R this can be obtained with the table function. Is there anything similar in python3?

like image 916
Donbeo Avatar asked Sep 07 '14 13:09

Donbeo


People also ask

Is there a table function in Python?

table() function in Python. Matplotlib. pyplot. table() is a subpart of matplotlib library in which a table is generated using the plotted graph for analysis.


1 Answers

Pandas has a built-in function called value_counts().

Example: if your DataFrame has a column with values as 0's and 1's, and you want to count the total frequencies for each of them, then simply use this:

df.colName.value_counts() 
like image 108
Shankar Chavan Avatar answered Sep 19 '22 14:09

Shankar Chavan