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
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 number of occurrences for each unique value
Yes, you can use GROUP BY
:
SELECT time,
activities,
COUNT(*)
FROM table
GROUP BY time, activities;
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.
Count unique occurrences within data frame
One option could be:
sapply(df, function(x) table(factor(x, levels = unique(unlist(df)))))
V1 v2 v3
A 1 1 2
B 1 2 0
D 1 0 1
C 0 1 1
How to count unique values a column in R
We can use n_distinct()
from dplyr
to count the number of unique values for a column in a data frame.
textFile <- "id var1
111 A
109 A
112 A
111 A
108 A"
df <- read.table(text = textFile,header = TRUE)
library(dplyr)
df %>% summarise(count = n_distinct(id))
...and the output:
> df %>% summarise(count = n_distinct(id))
count
1 4
We can also summarise the counts within one or more by_group()
columns.
textFile <- "id var1
111 A
109 A
112 A
111 A
108 A
201 B
202 B
202 B
111 B
112 B
109 B"
df <- read.table(text = textFile,header = TRUE)
df %>% group_by(var1) %>% summarise(count = n_distinct(id))
...and the output:
`summarise()` ungrouping output (override with `.groups` argument)
# A tibble: 2 x 2
var1 count
<chr> <int>
1 A 4
2 B 5
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)]
How to find out number of unique values in a column along with count of the unique values in a dataframe?
Use pd.value_counts
pd.value_counts(df.Account_Type)
Gold 3
Platinum 1
Name: Account_Type, dtype: int64
Get number of unique as well
s = pd.value_counts(df.Account_Type)
s1 = pd.Series({'nunique': len(s), 'unique values': s.index.tolist()})
s.append(s1)
Gold 3
Platinum 1
nunique 2
unique values [Gold, Platinum]
dtype: object
Alternate Approach
df['col1'].value_counts(sort=True)
df['col1'].value_counts(sort=True, normalize=True) -> provides proportion
Related Topics
Find Duplicated Elements With Dplyr
Mapping Columns/Rows from One Dataframe to Another Based on Row Number
Add Legend to Ggplot2 Line Plot
Ggplot2 Stacked Bar Chart - Each Bar Being 100% and With Percenage Labels Inside Each Bar
Multiplying All Columns in Dataframe by Single Column
Duplicating Rows in R Merge Function
Split an Audio File into Pieces of an Arbitrary Size
Combing a Categorical Variable to Create a New Categorical Variable in R
Conditional Replacement of a Comma With a Dot in a Numeric Column
Error in Confusion Matrix:The Data and Reference Factors Must Have the Same Number of Levels
How to Change the Spacing Between Legend Items in Ggplot2
Add Legend to Geom_Line() Graph in R
How to Filter Multiple Columns With Same Condition in R
Filter Rows Which Contain a Certain String
How to Reshape Data from Long to Wide Format
Run R Script from Command Line