Pandas get the most frequent values of a column
By using mode
df.name.mode()
Out[712]:
0 alex
1 helen
dtype: object
Get most frequent string from a data frame column
It seems like you need something like:
Function
freqfunc <- function(x, n){
tail(sort(table(unlist(strsplit(as.character(x), ", ")))), n)
}
Testing on your data set
freqfunc(gaps$MissingDates, 5) # Five most frequent dates
## 1996-12-26 1997-12-26 1998-01-02 1999-12-31 2001-09-12
## 4 4 4 4 4
Most frequent element from a data.frame
I would suggest a tidyverse
approach:
library(tidyr)
library(dplyr)
#Separate rows
df %>% separate_rows(`Most frequent`,2,sep = ',') %>%
group_by(`Most frequent`) %>%
summarise(N=n()) %>% arrange(desc(N))
Output:
Most frequent N
1 1110 24
2 110 22
3 310 19
4 8210 16
5 210 12
6 3210 6
7 9120 6
8 3830 5
9 5190 4
10 111 3
11 3110 3
12 3819 2
13 1120 1
14 27080618 1
15 9210 1
Which is similar to @AllanCameron results.
How to get the n most frequent or top values per column in python pandas?
This should work
n = 2
df.apply(lambda x: pd.Series(x.value_counts().index[:n]))
Dataframe - get most frequent values and their count
I believe you need DataFrame
with 2 columns filled by top10 values:
df1 = df['product'].value_counts().iloc[:10].rename_axis('val').reset_index(name='count')
How to get the most frequent row in table
In Pandas 1.1.0. is possible to use the method value_counts()
to count unique rows in DataFrame:
df.value_counts()
Output:
col_1 col_2 col_3
1 1 A 2
0 C 1
B 1
A 1
0 1 A 1
This method can be used to find the most frequent row:
df.value_counts().head(1).index.to_frame(index=False)
Output:
col_1 col_2 col_3
0 1 1 A
How to get the number of the most frequent value in a column?
It looks like you may have some nulls in the column. You can drop them with df = df.dropna(subset=['item'])
. Then df['item'].value_counts().max()
should give you the max counts, and df['item'].value_counts().idxmax()
should give you the most frequent value.
Get the most frequent entry
library(dplyr)
df %>%
group_by(Name) %>%
count(City) %>%
top_n(1)
# Selecting by n
# # A tibble: 2 x 3
# # Groups: Name [2]
# Name City n
# <chr> <chr> <int>
# 1 A New York 3
# 2 B Chicago 2
Related Topics
How to Render Custom Map Tiles Created with Gdal2Tiles in Leaflet for R
"Dims [Product Xx] Do Not Match the Length of Object [Xx]" Error in Using R Function 'Outer'
Choose Specific Number with Probability
Looping Over Combinations of Regression Model Terms
Why Does "Hello" > 0 Return True
Changing Names in a List of Dataframes
Why Does 1..99,999 == "1".."99,999" in R, But 100,000 != "100,000"
How to Plot Charts with Nested Categories Axes
R: Using "Microbenchmark" and Ggplot2 to Plot Runtimes
R Function That Uses Its Output as Its Own Input Repeatedly
Extracting HTML Table from a Website in R
How to Use Geom_Rect with Discrete Axis Values
Adding a New Column to Matrix Error
Error: Package or Namespace Load Failed for 'Car'
R: Miscellaneous Errors While Trying to Plot Graphs
R: How to Match/Join 2 Matrices of Different Dimensions (Nrow/Ncol)