Как посчитать медиану в pandas
Перейти к содержимому

Как посчитать медиану в pandas

  • автор:

pandas.DataFrame.median#

Return the median of the values over the requested axis.

Parameters axis

Axis for the function to be applied on. For Series this parameter is unused and defaults to 0.

skipna bool, default True

Exclude NA/null values when computing the result.

level int or level name, default None

If the axis is a MultiIndex (hierarchical), count along a particular level, collapsing into a Series.

Deprecated since version 1.3.0: The level keyword is deprecated. Use groupby instead.

Include only float, int, boolean columns. If None, will attempt to use everything, then use only numeric data. Not implemented for Series.

Deprecated since version 1.5.0: Specifying numeric_only=None is deprecated. The default value will be False in a future version of pandas.

Finding median of entire pandas Data frame

I’m trying to find the median flow of the entire dataframe. The first part of this is to select only certain items in the dataframe.

There were two problems with this, it included parts of the data frame that aren’t in ‘states’. Also, the median was not a single value, it was based on row. How would I get the overall median of all the data in the dataframe?

2 Answers 2

1) A pandas option:

2) A numpy option:

The DataFrame you pasted is slightly messy due to some spaces. But you’re going to want to melt the Dataframe and then use median() on the new melted Dataframe:

Your Dataframe may be slightly different, but the concept is the same. Check the comment that I left about to understand pd.melt() , especially the value_vars and id_vars arguments.

Here is a very detailed way of how I went about cleaning and getting the correct answer:

    The Overflow Blog
Related
Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.3.11.43304

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Pandas – Get Median of One or More Columns

The median of a set of numbers represents the middle value if the numbers are arranged in sorted order. It is a measure of central tendency and is often preferred over the mean as it’s not much affected by the presence of outliers. In this tutorial, we will look at how to get the median of one or more columns in a pandas dataframe.

How to calculate the median of pandas column values?

You can use the pandas median() function or the pandas quantile() function to get the median of column values in a pandas dataframe. The following is the syntax:

Let’s create a sample dataframe that we will be using throughout this tutorial to demonstrate the usage of the methods and syntax mentioned.

The sample dataframe is taken form a section of the Iris dataset. This sample has petal and sepal dimensions of eight data points of the “Setosa” species.

Median of a single column

First, let’s see how to get the median of a single dataframe column.

You can use the pandas series median() function to get the median of individual columns (which essentially are pandas series). For example, let’s get the median of the “sepal_length” column in the above dataframe.

You see that we get the median of all values in the “sepal_length” column as the scaler value 4.95.

Median of a single column using quantile()

Additionally, you can also use pandas quantile() function which gives the nth percentile value. Median is the 50th percentile value. So, to get the median with the quantile() function, pass 0.5 as the argument.

Median of more than one column

Use the pandas dataframe median() function to get the median values for all the numerical columns in the dataframe. For example, let’s get the median of all the numerical columns in the dataframe “df”

We get the result as a pandas series.

Median of more than one column using quantile()

Additionally, you can use the pandas dataframe quantile() function with an argument of 0.5 to get the median of all the numerical columns in a dataframe. Let’s use this function on the dataframe “df” created above.

You can see that we get the median of all the numerical columns present in the dataframe.

Note that you can also use the pandas describe() function to look at key statistics including the median values of the numerical columns in the dataframe.

Dataframe statistics including the median (50%) from the describe() function.

The median here is represented by the 50% value (that is, the value at the 50th percentile).

For more on the pandas dataframe median() function, refer to its documention.

With this, we come to the end of this tutorial. The code examples and results presented in this tutorial have been implemented in a Jupyter Notebook with a python (version 3.8.3) kernel having pandas version 1.0.5

Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.

Tutorials on getting statistics for pandas dataframe values –

Author

Piyush Raj

Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

About

Data Science Parichay is an educational website offering easy-to-understand tutorials on topics in Data Science with the help of clear and fun examples.

Basic statistics in pandas DataFrame

Once you have cleaned your data, you probably want to run some basic statistics and calculations on your pandas DataFrame. It is really easy. Below I show some of the most common and basic statistics that you may want to use — there is a whole lot more to explore!

In the below examples, I am using a dataset I downloaded from Kaggle: Climate Change: Earth Surface Temperatures (https://www.kaggle.com/berkeleyearth/climate-change-earth-surface-temperature-data)

To add all of the values in a particular column of a DataFrame (or a Series), you can do the following:

df[‘column_name’].sum()

The above function skips the missing values by default. However, you can define that by passing a skipna argument with either True or False:

df[‘column_name’].sum(skipna=True)

Arithmetic mean

df[‘column_name’].mean()

df.mean(axis=0)

Passing the argument of axis=0 returns the mean of every single column in the DataFrame:

df.mean(axis=1)

Passing the argument of axis=1 will return the mean of every single row in the DataFrame

Summary statistics

df[‘column_name’].describe()

This function gives you several useful things all at the same time. For example, you will get the three quartiles, mean, count, minimum and maximum values and the standard deviation. This is very useful, especially in exploratory data analysis.

df[‘column_name’].describe(percentiles=[percentile1, percentile2, percentile3, percentile4]

You can also choose specific percentiles to be included in the describe method output by adding the percentiles argument and specifying. You can change the number of percentiles you ask for as you please — 4 percentiles are just an example.

Note: If your object is non-numerical, the summary statistics will be sligthly different. They will include the count, frequency, the number of unique values and the top value.
If your object contains both numerical and non-numerical values, the describe method will only include summary statistics of the numerical values.

Counting the number of values

df[‘column_name’].count()

Here, you will get the number of values you have in the column.

Maximum and minimum value

df[‘column_name’].max()

Finding the maximum value from a column of a DataFrame or a Series. You probably get the idea by now.

df[‘column_name’].min()

Finding the minimum value from the column of a DataFrame or a Series.

Median

df[‘column_name’].median()

Finding the median:

df[‘column_name’].mode()

Finding the mode:

Of course, there are a lot of other statistics you may need to use — rolling mean, variance or standard deviation to mention just a few.

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *