Questions tagged [count]

Count refers to the number of objects in a collection. It's also a commonly-used SQL function that counts the number of rows.

Many programming languages offer some sort of count, length or size fields and/or methods for arrays and collections, e.g. PHP's count($array) or Java's array.length.

COUNT() is also commonly-used SQL function that counts the number of rows in a table. It is an ANSI SQL aggregate function that returns the number of times the argument is encountered per group (or if no non-aggregate columns, per query). Commonly, the argument is specified as * - ie COUNT(*) - simply counting the rows. It can be used to count distinct values of a column like this: COUNT(DISTINCT MY_COLUMN)

Reference

See also:

21460 questions
2125
votes
30 answers

How do I count the occurrences of a list item?

Given a single item, how do I count occurrences of it in a list, in Python? A related but different problem is counting occurrences of each different element in a collection, getting a dictionary or list as a histogram result instead of a single…
weakish
  • 28,682
  • 5
  • 48
  • 60
1968
votes
20 answers

How to efficiently count the number of keys/properties of an object in JavaScript

What's the fastest way to count the number of keys/properties of an object? Is it possible to do this without iterating over the object? I.e., without doing: var count = 0; for (k in myobj) if (myobj.hasOwnProperty(k)) ++count; (Firefox did…
mjs
  • 63,493
  • 27
  • 91
  • 122
1271
votes
26 answers

Count the number of occurrences of a character in a string

How do I count the number of occurrences of a character in a string? e.g. 'a' appears in 'Mary had a little lamb' 4 times.
Mat
  • 82,161
  • 34
  • 89
  • 109
682
votes
7 answers

Find duplicate lines in a file and count how many time each line was duplicated?

Suppose I have a file similar to the following: 123 123 234 234 123 345 I would like to find how many times '123' was duplicated, how many times '234' was duplicated, etc. So ideally, the output would be like: 123 3 234 2 345 1
user839145
  • 6,883
  • 3
  • 16
  • 10
617
votes
32 answers

How do I count the occurrence of a certain item in an ndarray?

How do I count the number of 0s and 1s in the following array? y = np.array([0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1]) y.count(0) gives: numpy.ndarray object has no attribute count
mflowww
  • 6,835
  • 6
  • 18
  • 18
569
votes
21 answers

How do you find the row count for all your tables in Postgres

I'm looking for a way to find the row count for all my tables in Postgres. I know I can do this one table at a time with: SELECT count(*) FROM table_name; but I'd like to see the row count for all the tables and then order by that to get an idea…
mmrobins
  • 12,809
  • 7
  • 41
  • 42
542
votes
12 answers

jQuery: count number of rows in a table

How do I count the number of tr elements within a table using jQuery? I know there is a similar question, but I just want the total rows.
danjan
490
votes
16 answers

Count the frequency that a value occurs in a dataframe column

I have a dataset category cat a cat b cat a I'd like to return something like the following which shows the unique values and their frequencies category freq cat a 2 cat b 1
yoshiserry
  • 20,175
  • 35
  • 77
  • 104
484
votes
20 answers

Counting the number of elements with the values of x in a vector

I have a vector of numbers: numbers <- c(4,23,4,23,5,43,54,56,657,67,67,435, 453,435,324,34,456,56,567,65,34,435) How can I have R count the number of times a value x appears in the vector?
RQuestions
  • 4,843
  • 3
  • 16
  • 4
401
votes
11 answers

Pandas 'count(distinct)' equivalent

I am using Pandas as a database substitute as I have multiple databases (Oracle, SQL Server, etc.), and I am unable to make a sequence of commands to a SQL equivalent. I have a table loaded in a DataFrame with some columns: YEARMONTH, CLIENTCODE,…
Adriano Almeida
  • 5,186
  • 5
  • 20
  • 28
371
votes
31 answers

How to find length of digits in an integer?

In Python, how do you find the number of digits in an integer?
Strigoides
  • 4,329
  • 5
  • 23
  • 25
360
votes
30 answers

How to count the number of files in a directory using Python

How do I count only the files in a directory? This counts the directory itself as a file: len(glob.glob('*'))
prosseek
  • 182,215
  • 215
  • 566
  • 871
351
votes
42 answers

Counting the occurrences / frequency of array elements

In Javascript, I'm trying to take an initial array of number values and count the elements inside it. Ideally, the result would be two new arrays, the first specifying each unique element, and the second containing the number of times each element…
Jack W
  • 3,781
  • 4
  • 17
  • 9
311
votes
6 answers

Counting the number of distinct keys in a dictionary in Python

I have a a dictionary mapping keywords to the repetition of the keyword, but I only want a list of distinct words so I wanted to count the number of keywords. Is there a way to count the number of keywords or is there another way I should look for…
Dan
  • 8,263
  • 16
  • 51
  • 53
300
votes
21 answers

Select count(*) from multiple tables

How can I select count(*) from two different tables (call them tab1 and tab2) having as result: Count_1 Count_2 123 456 I've tried this: select count(*) Count_1 from schema.tab1 union all select count(*) Count_2 from schema.tab2 But all I…
user73118
  • 3,011
  • 2
  • 17
  • 4
1
2 3
99 100