Pandas Series mode() Function:
Pandas series mode: The mode() function of the Pandas Series returns the mode/modes of the given Series.
Syntax:
Series.mode(dropna=None)
Parameters
dropna: This is optional. When computing the result, specify True to exclude NA/null values. True is the default value.
Return Value:
Mode pandas: The mode/modes of the Series in sorted order is returned by the mode() function of the Pandas Series
- Python Pandas Series ge() Function
- Python Pandas Series abs() Function
- Python Pandas Series eq() Function
Pandas Series mode() Function in Python
Example1
Approach:
- Import pandas module using the import keyword.
- Import numpy module using the import keyword.
- Give the category(level) values as arguments list to from_arrays() functions
- Pass some random list, index values from the above and name as Numbers as the arguments to the Series() function of the pandas module to create a series.
- Store it in a variable.
- Print the above-given series
- Apply mode() function on the given series to get the mode values of all the elements of the given series and print the result.
-
The Exit of the Program.
Below is the implementation:
# Import pandas module using the import keyword.
import pandas as pd
# Import numpy module using the import keyword.
import numpy as np
# Give the category(level) values as arguments list to from_arrays() functions
gvn_indx = pd.MultiIndex.from_arrays([
['positive', 'negative', 'positive',
'positive', 'negative', 'negative']],
names=['DataType'])
# Pass some random list, index values from the above and name as Numbers
# as the arguments to the Series() function of the pandas module to create a series.
# Store it in a variable.
gvn_series = pd.Series([12, 3, 12, 14, 56, 3],
name='Numbers', index=gvn_indx)
# Print the above given series
print("The given series is:")
print(gvn_series)
print()
# Apply mode() function on the given series to
# get the mode values of all the elements of the given series
# and print the result.
print("The mode values of all the elements of the given series:")
print(gvn_series.mode())
Output:
The given series is: DataType positive 12 negative 3 positive 12 positive 14 negative 56 negative 3 Name: Numbers, dtype: int64 The mode values of all the elements of the given series: 0 3 1 12 dtype: int64
Example2
Approach:
- Import pandas module using the import keyword.
- Pass some random key-value pair(dictionary), index list as arguments to the DataFrame() function of the pandas module to create a dataframe.
- Store it in a variable.
- Print the given dataframe.
- Apply mode() function on the student_marks column of the dataframe to get the mode of the student_marks column of the dataframe and print the result.
-
The Exit of the Program.
Below is the implementation:
# Import pandas module using the import keyword.
import pandas as pd
# Pass some random key-value pair(dictionary), index list as arguments to the
# DataFrame() function of the pandas module to create a dataframe
# Store it in a variable.
data_frme = pd.DataFrame({
"student_rollno": [1, 2, 3, 4],
"student_marks": [80, 35, 25, 90]},
index= ["virat", "nick" , "jessy", "sindhu"]
)
# Print the given dataframe
print("The given Dataframe:")
print(data_frme)
print()
# Apply mode() function on the student_marks column of the dataframe to
# get the mode of the student_marks column of the dataframe
# and print the result.
print("The mode of student_marks column of the dataframe:")
print(data_frme["student_marks"].mode())
Output:
The given Dataframe:
student_rollno student_marks
virat 1 80
nick 2 35
jessy 3 25
sindhu 4 90
The mode of student_marks column of the dataframe:
0 25
1 35
2 80
3 90
dtype: int64