Pandas Descriptive Statistics
Descriptive statistics are used to summarize and describe the basic characteristics of data. Pandas provides a rich set of statistical functions for quickly obtaining the overall distribution of data.
Basic statistical functions
Example
import
pandas
as
pd
import
numpy
as
np
Create sample data
df
=
pd.
DataFrame
(
{
"Age"
:
[
25
,
30
,
28
,
35
,
22
,
40
,
38
,
45
,
29
,
31
]
,
"Salary"
:
[
12000
,
15000
,
11000
,
18000
,
9000
,
20000
,
17000
,
22000
,
12500
,
14000
]
,
"Performance"
:
[
85
,
92
,
78
,
88
,
65
,
95
,
82
,
90
,
75
,
89
]
}
)
(
"=== Data Overview ==="
)
(
f
"Data shape: {df.shape}"
)
(
f
"\nBasic statistics:"
)
(
df.
describe
(
)
)
Common statistical measures
| function | Description | Example |
|---|---|---|
count() |
Number of non-null values | df["年龄"].count() |
sum() |
Sum | df["薪资"].sum() |
mean() |
Mean | df["年龄"].mean() |
median() |
Median | df["年龄"].median() |
std() |
Standard deviation | df["绩效"].std() |
var() |
Variance | df["绩效"].var() |
min() / max() |
Minimum/maximum | df["年龄"].min() |
quantile() |
Quantiles | df["薪资"].quantile(0.25) |
Calculate quantiles
Example
import
pandas
as
pd
import
numpy
as
np
s
=
pd.
Series
(
range
(
1
,
101
)
)
1 to 100
(
"Various quantiles:"
)
(
f
"0% (minimum): {s.quantile(0)}"
)
(
f
"25%: {s.quantile(0.25)}"
)
(
f
"50% (median): {s.quantile(0.50)}"
)
(
f
"75%: {s.quantile(0.75)}"
)
(
f
"100% (maximum): {s.quantile(1)}"
)
(
)
Calculate multiple quantiles at once
(
"Batch quantiles:"
)
(
s.
quantile
(
[
0.1
,
0.2
,
0.5
,
0.8
,
0.9
]
)
)
describe summary
describe() Provides a comprehensive statistical summary of the numerical columns in a dataset.
Example
import
pandas
as
pd
df
=
pd.
DataFrame
(
{
"Age"
:
[
25
,
30
,
28
,
35
]
,
"Salary"
:
[
12000
,
15000
,
11000
,
18000
]
,
"City"
:
[
"Beijing"
,
"Shanghai"
,
"Guangzhou"
,
"Shenzhen"
]
}
)
(
"Numerical column statistics:"
)
(
df.
describe
(
)
)
(
)
(
"Including all columns:"
)
(
df.
describe
(
include
=
"all"
)
)
Correlation coefficient and covariance
Example
import
pandas
as
pd
import
numpy
as
np
df
=
pd.
DataFrame
(
{
"Age"
:
[
25
,
30
,
28
,
35
,
22
]
,
"Salary"
:
[
12000
,
15000
,
11000
,
18000
,
9000
]
,
"Performance"
:
[
85
,
92
,
78
,
88
,
65
]
}
)
(
"Correlation coefficient matrix:"
)
(
df.
corr
(
)
)
(
)
Correlation with a specific column
(
"Correlation with salary:"
)
(
df.
corr
(
)
[
"Salary"
]
)
The correlation coefficient ranges from -1 to 1; closer to 1 indicates a positive correlation, closer to -1 a negative correlation, and close to 0 no correlation.
Advanced statistics
Example
import
pandas
as
pd
import
numpy
as
np
s
=
pd.
Series
(
[
1
,
2
,
3
,
4
,
5
,
6
,
7
,
8
,
9
,
10
]
)
Skewness (measures the degree of asymmetry of a distribution)
(
f
"Skewness: {s.skew()}"
)
Kurtosis (measures the degree of peakedness of a distribution)
(
f
"Kurtosis: {s.kurtosis()}"
)
(
)
Cumulative statistics
s
=
pd.
Series
(
[
1
,
2
,
3
,
4
,
5
]
)
(
"Cumulative sum:"
)
(
s.
cumsum
(
)
)
(
)
(
"Cumulative maximum:"
)
(
s.
cummax
(
)
)