Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I count the number of times a value occurs in a column of a dataframe?

Tags:

r

statistics

Is there a simple way of identifying the number of times a value is in a vector or column of dataframe? I essentially want the numerical values of a histogram but I do not know how to access it.

# sample vector
a <- c(1,2,1,1,1,3,1,2,3,3)

#hist
hist(a)

Thank you.

UPDATE:

On Dirk's suggestion I am using hist. Is there a better way than than specifying the range as 1.9, 2.9 etc when I know that all my values are integers?

 hist(a, breaks=c(1,1.9,2.9,3.9,4.9,5.9,6.9,7.9,8.9,9.9), plot=FALSE)$counts
like image 634
djq Avatar asked Jul 05 '10 20:07

djq


People also ask

How do you count the number of occurrences of a value in a DataFrame?

Using the size() or count() method with pandas. DataFrame. groupby() will generate the count of a number of occurrences of data present in a particular column of the dataframe.

How do you count occurrences in a column in pandas?

To count the number of occurrences in e.g. a column in a dataframe you can use Pandas value_counts() method. For example, if you type df['condition']. value_counts() you will get the frequency of each unique value in the column “condition”.

How do I get a count of values in a column?

Use the COUNTIF function to count how many times a particular value appears in a range of cells.

How do you count the frequency of a value in a DataFrame Python?

To count the frequency of a value in a DataFrame column in Pandas, we can use df. groupby(column name). size() method.


1 Answers

Use table function.

like image 56
mbq Avatar answered Oct 15 '22 19:10

mbq