This article is participating in Python Theme Month. See [activities]
The table used in this article is as follows:
Let’s look at the original situation:
Import pandas as pd df = pd.read_excel(r 'c :\Users\admin\Desktop\ test.xlsx ') print(df)Copy the code
result:
Classified Goods Physical store Sales Volume Online Cost of sales Selling Price 0 Fruit Apple 34 234 12 45 1 Home appliance TV 56 784 34 156 2 home appliance refrigerator 78 345 24 785 3 Books Python from getting started to giving up 25 34 13 89 4 Fruit grapes 789 56 7 398Copy the code
1. Take the average
1.1 Operating the Full Table
1.1.1 Calculate the average of each column
Df = pd.read_excel(r'C:\Users\admin\Desktop\ test.xlsx ') print(df.mean())Copy the code
result:
Physical store sales 196.4 Online sales 290.6 Cost 18.0 Selling price 294.6 dType: Float64Copy the code
1.1.2 Take the average of each row
Df = pd.read_excel(r'C:\Users\admin\Desktop\ test.xlsx ') print(df.mean(axis=1))Copy the code
result:
0 81.25
1 257.50
2 308.00
3 40.25
4 312.50
dtype: float64
Copy the code
Looking at the results, we can see that the average of each row ignores the columns of the text character type and averages only the columns of the numeric type. Like the first row of data
Classified goods physical store sales online cost of sales selling price 0 fruit apple 34 234 12 45Copy the code
The top 81.25 is equal to (34+234+12+45) / 4, and the rest of the rows are the same
1.2 Operate on a single row or column
1.2.1 Average a single column
Df = pd.read_excel(r'C:\Users\admin\Desktop\ test.xlsx ') print(df[' sales '].mean())Copy the code
result:
196.4
Copy the code
1.2.2 Average a single row
Df = pd.read_excel(r'C:\Users\admin\Desktop\ test.xlsx ') print(df.iloc[[0]].mean())Copy the code
result:
In-store sales 34.0 Online sales 234.0 Cost 12.0 Selling Price 45.0 DType: Float64Copy the code
1.3 Operate on multiple rows or columns
1.3.1 Take the average of multiple columns
Df = pd.read_excel(r'C:\Users\admin\Desktop\ test.xlsx ') print(df[[' sales ', 'sales'].mean())Copy the code
result:
Physical store sales 196.4 Online sales 290.6 dType: Float64Copy the code
1.3.2 Take the average of multiple lines
Df = pd.read_excel(r'C:\Users\admin\Desktop\ test.xlsx ') print(df.iloc[[0, 1]].mean())Copy the code
result:
Physical store sales volume 45.0 Online sales volume 509.0 Cost 23.0 Price 100.5 dType: Float64Copy the code
2 is the median
2.1 Performing Operations on the Full Table
2.1.1 Calculate the median of each column
Df = pd.read_excel(r'C:\Users\admin\Desktop\ test.xlsx ') print(df.median())Copy the code
result:
In-store sales volume 56.0 Online sales volume 234.0 Cost 13.0 Selling price 156.0 dType: Float64Copy the code
As you can see, the median concept only works for numbers
2.1.2 Calculate the median of each row
Df = pd.read_excel(r'C:\Users\admin\Desktop\ test.xlsx ') print(df.median(axis=1))Copy the code
result:
0 39.5
1 106.0
2 211.5
3 29.5
4 227.0
dtype: float64
Copy the code
2.2 Operate on a single row or column
2.2.1 Calculate the median of a column
Df = pd.read_excel(r'C:\Users\admin\Desktop\ test.xlsx ') print(df[' median() '].median())Copy the code
result:
56.0
Copy the code
2.2.2 Calculate the median of a row
Df = pd.read_excel(r'C:\Users\admin\Desktop\ test.xlsx ') print(df.iloc[[0]].median())Copy the code
result:
In-store sales 34.0 Online sales 234.0 Cost 12.0 Selling Price 45.0 DType: Float64Copy the code
2.3 Perform operations on multiple rows or columns
2.3.1 Calculate the median of multiple columns
Df = pd.read_excel(r'C:\Users\admin\Desktop\ test.xlsx ') print(df[[' median ', 'median']].median())Copy the code
result:
Physical store sales 56.0 Online sales 234.0 dType: Float64Copy the code
2.3.2 Calculate the median for multiple rows
Df = pd.read_excel(r'C:\Users\admin\Desktop\ test.xlsx ') print(df.iloc[[0, 1]].median())Copy the code
result:
Physical store sales volume 45.0 Online sales volume 509.0 Cost 23.0 Price 100.5 dType: Float64Copy the code