SoFunction
Updated on 2024-11-18

Examples of pandas methods for finding the mean and median

intend

pandas is a powerful toolkit for Python data analysis.

pandas is built on NumPy.

Main functions of pandas

  • The data structures DataFrame, Series, which have the ability to
  • Integrated time series functionality
  • Provides rich math operations and manipulations
  • Flexible handling of missing data

The table used in this article reads as follows.

Let's take a look at the original scenario.

import pandas as pd
​
df = pd.read_excel(r'C:\Users\admin\Desktop\Test.xlsx')
print(df)

result:

Category Goods Physical store sales volume Online sales volume Cost Selling price
0 Fruits Apples 34 234 12 45
1 Household appliances Television sets 56 784 34 156
2 Home appliances Refrigerator 78 345 24 785
3 books python from start to quit 25 34 13 89
4 Fruits Grapes 789 56 7 398

1. Finding the average

1.1 Operate on the full table

1.1.1 Taking the average of each column
df = pd.read_excel(r'C:\Users\admin\Desktop\Test.xlsx')
print(())

result:

Brick-and-mortar sales 196.4
Online sales 290.6
Cost 18.0
Price 294.6
dtype: float64

1.1.2 Taking the average of each row

df = pd.read_excel(r'C:\Users\admin\Desktop\Test.xlsx')
print((axis=1))

result:

0     81.25
1    257.50
2    308.00
3     40.25
4    312.50
dtype: float64

First look at the results of the run, we can see that each row of the average directly ignore the text character type of columns, only the number of types of columns for the average. For example, the first line of data

Category Goods Physical store sales volume Online sales volume Cost Selling price
0 Fruits Apples 34 234 12 45

81.25 above = (34+234+12+45) / 4,, and the other rows are the same

1.2 Operating on a separate row or column

1.2.1 Taking the average of a separate column

df = pd.read_excel(r'C:\Users\admin\Desktop\Test.xlsx')
print(df['Brick-and-mortar sales'].mean())

result:

196.4

1.2.2 Finding the average of a separate row

df = pd.read_excel(r'C:\Users\admin\Desktop\Test.xlsx')
print([[0]].mean())

result:

Brick-and-mortar sales 34.0
Online sales volume 234.0
Cost 12.0
Price 45.0
dtype: float64

1.3 Operating on multiple rows or columns

1.3.1 Taking the average of multiple columns

df = pd.read_excel(r'C:\Users\admin\Desktop\Test.xlsx')
print(df[['Brick-and-mortar sales', "Online sales volume"]].mean())

result:

Brick-and-mortar sales 196.4
Online sales 290.6
dtype: float64

1.3.2 Taking the average of multiple rows

df = pd.read_excel(r'C:\Users\admin\Desktop\Test.xlsx')
print([[0, 1]].mean())

result:

Brick-and-mortar sales 45.0
Online sales volume 509.0
Cost 23.0
Selling price 100.5
dtype: float64

2 Finding the median

2.1 Operate on the whole table

2.1.1 Finding the median for each column

df = pd.read_excel(r'C:\Users\admin\Desktop\Test.xlsx')
print(())

result:

Physical store sales 56.0
Online sales volume 234.0
Cost 13.0
Price 156.0
dtype: float64

As you can see, the concept of median is only valid for numbers

2.1.2 Finding the median for each row

df = pd.read_excel(r'C:\Users\admin\Desktop\Test.xlsx')
print((axis=1))

result:

0     39.5
1    106.0
2    211.5
3     29.5
4    227.0
dtype: float64

2.2 Operating on a separate row or column

2.2.1 Finding the median of a column

df = pd.read_excel(r'C:\Users\admin\Desktop\Test.xlsx')
print(df['Brick-and-mortar sales'].median())

result:

56.0

2.2.2 Finding the median of a row

df = pd.read_excel(r'C:\Users\admin\Desktop\Test.xlsx')
print([[0]].median())

result:

Brick-and-mortar sales 34.0
Online sales volume 234.0
Cost 12.0
Price 45.0
dtype: float64

2.3 Operating on multiple rows or columns

2.3.1 Finding the median for multiple columns

df = pd.read_excel(r'C:\Users\admin\Desktop\Test.xlsx')
print(df[['Brick-and-mortar sales', "Online sales volume"]].median())

result:

Physical store sales 56.0
Online sales volume 234.0
dtype: float64

2.3.2 Finding the median over multiple rows

df = pd.read_excel(r'C:\Users\admin\Desktop\Test.xlsx')
print([[0, 1]].median())

result:

Brick-and-mortar sales 45.0
Online sales volume 509.0
Cost 23.0
Selling price 100.5
dtype: float64

summarize

to this article on pandas mean and median of the article is introduced to this, more related pandas mean median content please search for my previous articles or continue to browse the following related articles I hope you will support me in the future!