Count number of occurrences for each unique value
Yes, you can use GROUP BY
:
SELECT time,
activities,
COUNT(*)
FROM table
GROUP BY time, activities;
Get number of occurrences of each unique value
df %>% select(val) %>% group_by(val) %>% mutate(count = n()) %>% unique()
This first filters out the value of interest, groups by it, then creates a new column all the unique values, and the number of occurrences of each of those values.
Here is a reproducible example showcasing how it works:
id <- c(1,2,3,4,5,6,7,8,9,0)
val <- c(0,1,2,3,1,1,1,0,0,2)
df <- data.frame(id=id,val=val)
df
#> id val
#> 1 1 0
#> 2 2 1
#> 3 3 2
#> 4 4 3
#> 5 5 1
#> 6 6 1
#> 7 7 1
#> 8 8 0
#> 9 9 0
#> 10 0 2
df %>% select(val) %>% group_by(val) %>% mutate(count = n()) %>% unique()
#> # A tibble: 4 x 2
#> # Groups: val [4]
#> val count
#> <dbl> <int>
#> 1 0 3
#> 2 1 4
#> 3 2 2
#> 4 3 1
Created on 2020-06-17 by the reprex package (v0.3.0)
Count occurrences of each unique value within a column pandas
Try value_counts
df["Month"].value_counts()
Count the occurrences of DISTINCT values
SELECT name,COUNT(*) as count
FROM tablename
GROUP BY name
ORDER BY count DESC;
Count number of occurences for each unique value
Perhaps table is what you are after?
dummyData = rep(c(1,2, 2, 2), 25)
table(dummyData)
# dummyData
# 1 2
# 25 75
## or another presentation of the same data
as.data.frame(table(dummyData))
# dummyData Freq
# 1 1 25
# 2 2 75
how to count occurrence of each unique value in pandas
It seems like you want to compute value counts across all columns. You can flatten it to a series, drop NaNs, and call value_counts
. Here's a sample -
df
a b
0 1.0 NaN
1 1.0 NaN
2 3.0 3.0
3 NaN 4.0
4 5.0 NaN
5 NaN 4.0
6 NaN 5.0
pd.Series(df.values.ravel()).dropna().value_counts()
5.0 2
4.0 2
3.0 2
1.0 2
dtype: int64
Another method is with np.unique
-
u, c = np.unique(pd.Series(df.values.ravel()).dropna().values, return_counts=True)
pd.Series(c, index=u)
1.0 2
3.0 2
4.0 2
5.0 2
dtype: int64
Note that the first method sorts results in descending order of counts, while the latter does not.
How to get unique values with respective occurrence count from a list in Python?
If your items are grouped (i.e. similar items come together in a bunch), the most efficient method to use is itertools.groupby
:
>>> [(g[0], len(list(g[1]))) for g in itertools.groupby(['a', 'a', 'b', 'b', 'b'])]
[('a', 2), ('b', 3)]
Counting occurrences of unique values in a column using sql
SELECT ColumnName, COUNT(*)
FROM TableName
GROUP BY ColumnName
How to get the count of each distinct value in a column?
SELECT
category,
COUNT(*) AS `num`
FROM
posts
GROUP BY
category
Related Topics
How to Get The Old, Free Version of Anjlab's SQL Profiler
Atomically Mark and Return a Group of Rows in Database
Preventing Deadlocks in SQL Server
Is Limit Clause in Hive Really Random
Sql Join Using a Mapping Table
Difference Between on and Where Clauses in SQL Table Joins
Query Temp Table in Stored Proc Whilst Debugging in SQL 2008 Management Studio
Cannot Drop a System-Generated Sequence
Read Text File to Insert Data into Oracle SQL Table
Convert Datetime to Unix Timestamp
Using SQL Query to Determine If a Table Exists
Cannot Connect to Azure SQL Database, Even with Whitelisted Ip
Could Not Find Server 'server Name' in Sys.Servers. SQL Server 2014
How to Convert a SQL Subquery to a Join