Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Equivalent of "table" of R in python

Tags:

python

r

In R we can find the frequency of each item using table. This is an example in R:

x <- c(1,1,1,1,2,2)
y <- c("a","a","b","a","a","b")
table(x,y)
#   y
#x   a b
#  1 3 1
#  2 1 1

How can I implement it in python while x and y are as DataFrame? I am totally new in Python and I searched a lot but I was unable to find my answer. I should mention that I read this article but I couldn't implement it in my case?

like image 246
Hadij Avatar asked Jan 14 '18 17:01

Hadij


People also ask

Is there a table function in Python?

Python offers the ability to easily turn certain tabular data types into nicely formatted plain-text tables, and that's with the tabulate function.

Which function is equivalent to R in Python?

The pandas package for Python also has a function called apply, which is equivalent to its R counterpart; the following code illustrates how to use it.

What is a table in Python?

Defining a table means defining the names and datatypes of the attributes as well as the constraints to be applied to those attributes. Both MATLAB and Python use the same syntax define tables.

What is NCOL in Python?

The nrow() function returns the number of rows present in a given data frame, while the ncol() function returns the number of columns present in a data frame.


2 Answers

We can do this with crosstab from pandas

import numpy as np;
import pandas as pd;
x = np.array([1, 1, 1, 1, 2, 2]);
y = np.array(["a", "a", "b", "a", "a", "b"]);
pd.crosstab(x, y, rownames = ['x'], colnames = ['y']);
#  y  a  b
#x
#1  3  1
#2  1  1
like image 81
akrun Avatar answered Sep 23 '22 16:09

akrun


counting occurrences R:

sort(table(df$source), decreasing = TRUE)

Python Pandas:

df.source.value_counts() 
#or
df["source"].value_counts()

Source: R vs Python - a One-on-One Comparison


For counting occurrences between two columns

with R

table(cdc$gender,cdc$smoke100)

with python

pd.crosstab(index=df['gender'], columns=df['smoke100'])

Source: look at this answer

like image 36
anasmorahhib Avatar answered Sep 19 '22 16:09

anasmorahhib