Pandas Data Series: Compute the minimum, 25th percentile, median, 75th, and maximum of a given series
Pandas: Data Series Exercise-18 with Solution
Write a Pandas program to compute the minimum, 25th percentile, median, 75th, and maximum of a given series.
Sample Solution :
Python Code :
import pandas as pd
import numpy as np
num_state = np.random.RandomState(100)
num_series = pd.Series(num_state.normal(10, 4, 20))
print("Original Series:")
print(num_series)
result = np.percentile(num_series, q=[0, 25, 50, 75, 100])
print("\nMinimum, 25th percentile, median, 75th, and maximum of a given series:")
print(result)
Sample Output:
Original Series: 0 3.000938 1 11.370722 2 14.612143 3 8.990256 4 13.925283 5 12.056875 6 10.884719 7 5.719827 8 9.242017 9 11.020006 10 8.167892 11 11.740654 12 7.665620 13 13.267388 14 12.690883 15 9.582355 16 7.874878 17 14.118931 18 8.247458 19 5.526727 dtype: float64 Minimum, 25th percentile, median, 75th, and maximum of a given series: [ 3.00093811 8.09463867 10.23353705 12.21537733 14.61214321]
Explanation:
In the above exercise -
num_state = np.random.RandomState(100): This code creates a NumPy RandomState object 'num_state' with a seed value of 100.
num_series = pd.Series(num_state.normal(10, 4, 20)): This code creates a Pandas Series object 'num_series' containing 20 random values generated from a normal distribution with a mean of 10 and a standard deviation of 4 using the num_state.normal() method.
result = np.percentile(num_series, q=[0, 25, 50, 75, 100]): This code calculates the percentiles of the values in the Pandas Series object 'num_series' using the np.percentile() function. The 'q' parameter specifies the percentiles to calculate, with the values [0, 25, 50, 75, 100] indicating the minimum value, the lower quartile (25th percentile), the median (50th percentile), the upper quartile (75th percentile), and the maximum value, respectively.
Python-Pandas Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a Pandas program to get the items which are not common of two given series.
Next: Write a Pandas program to calculate the frequency counts of each unique value of a given series.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://198.211.115.131/python-exercises/pandas/python-pandas-data-series-exercise-18.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics