Calculate Mode in Python (4 Examples) | List & pandas DataFrame Column | Compute One / All Variables

Published: 22 March 2023
on channel: Statistics Globe
399
4

How to find the mode in the Python programming language. More details: https://statisticsglobe.com/calculate...
Python code of this video:

my_list = ['x', 'x', 'y', 'x', 'z', 'z'] # Create example list
print(my_list) # Print example list

import statistics # Import statistics module

print(statistics.mode(my_list)) # Get mode of list

import pandas as pd # Import pandas library

data = pd.DataFrame({'x1':[5, 2, 7, 3, 4, 4, 2, 3, 2, 1, 2, 5], # Create pandas DataFrame
'x2':['y', 'x', 'x', 'z', 'x', 'y', 'y', 'x', 'z', 'x', 'z', 'x'],
'group':['A', 'C', 'B', 'B', 'A', 'C', 'A', 'A', 'C', 'B', 'B', 'A']})
print(data) # Print pandas DataFrame

print(data['x1'].mode()) # Get mode of one column

print(data.mode()) # Get mode of all columns

print(data.groupby('group').agg(lambda x:x.value_counts().index[0])) # Get mode by group

Follow me on Social Media:
Facebook – Statistics Globe Page:   / statisticsglobecom  
Facebook – R Programming Group for Discussions & Questions:   / statisticsglobe  
Facebook – Python Programming Group for Discussions & Questions:   / statisticsglobepython  
LinkedIn – Statistics Globe Page:   / statisticsglobe  
LinkedIn – R Programming Group for Discussions & Questions:   / 12555223  
LinkedIn – Python Programming Group for Discussions & Questions:   / 12673534  
Twitter:   / joachimschork  

Music by bensound.com


Watch video Calculate Mode in Python (4 Examples) | List & pandas DataFrame Column | Compute One / All Variables online, duration hours minute second in high quality that is uploaded to the channel Statistics Globe 22 March 2023. Share the link to the video on social media so that your subscribers and friends will also watch this video. This video clip has been viewed 399 times and liked it 4 visitors.