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?
Python offers the ability to easily turn certain tabular data types into nicely formatted plain-text tables, and that's with the tabulate function.
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.
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.
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.
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With