Python数据分析基础
Series
是个定长的字典序列,有两个基本属性:index和value
import pandas as pd
from pandas import Series, DataFrame
x1 = Series([1,2,3,4])
x2 = Series(data=[1,2,3,4], index=['a', 'b', 'c', 'd'])
print(x1)
print(x2)
0 1
1 2
2 3
3 4
dtype: int64
a 1
b 2
c 3
d 4
dtype: int64
x1 中的 index 采用的是默认值,x2 中 index 进行了指定。我们也可以采用字典的方式来创建 Series,比如:
d = {'a':1, 'b':2, 'c':3, 'd':4}
x3 = Series(d)
print(x3)
a 1
b 2
c 3
d 4
dtype: int64
DataFrame
数据结构类似数据库表。
data = {'Chinese': [66, 95, 93, 90,80],'English': [65, 85, 92, 88, 90],'Math': [30, 98, 96, 77, 90]}
df1= DataFrame(data)
df2 = DataFrame(data, index=['ZhangFei', 'GuanYu', 'ZhaoYun', 'HuangZhong', 'DianWei'], columns=['English', 'Math', 'Chinese'])
print(df1)
print(df2)
Chinese English Math
0 66 65 30
1 95 85 98
2 93 92 96
3 90 88 77
4 80 90 90
English Math Chinese
ZhangFei 65 30 66
GuanYu 85 98 95
ZhaoYun 92 96 93
HuangZhong 88 77 90
DianWei 90 90 80
引入一下外部的数据,并加入字段名,显示成表格
from sklearn.datasets import load_iris
iris=load_iris()
df_iris = DataFrame(iris.data, columns=['sepal_length', 'sepa_width', 'peta_length', 'peta_width'])
print(df_iris)
sepal_length sepa_width peta_length peta_width
0 5.1 3.5 1.4 0.2
1 4.9 3.0 1.4 0.2
2 4.7 3.2 1.3 0.2
3 4.6 3.1 1.5 0.2
4 5.0 3.6 1.4 0.2
5 5.4 3.9 1.7 0.4
6 4.6 3.4 1.4 0.3
7 5.0 3.4 1.5 0.2
8 4.4 2.9 1.4 0.2
9 4.9 3.1 1.5 0.1
10 5.4 3.7 1.5 0.2
11 4.8 3.4 1.6 0.2
12 4.8 3.0 1.4 0.1
13 4.3 3.0 1.1 0.1
14 5.8 4.0 1.2 0.2
15 5.7 4.4 1.5 0.4
16 5.4 3.9 1.3 0.4
17 5.1 3.5 1.4 0.3
18 5.7 3.8 1.7 0.3
19 5.1 3.8 1.5 0.3
20 5.4 3.4 1.7 0.2
21 5.1 3.7 1.5 0.4
22 4.6 3.6 1.0 0.2
23 5.1 3.3 1.7 0.5
24 4.8 3.4 1.9 0.2
25 5.0 3.0 1.6 0.2
26 5.0 3.4 1.6 0.4
27 5.2 3.5 1.5 0.2
28 5.2 3.4 1.4 0.2
29 4.7 3.2 1.6 0.2
.. ... ... ... ...
120 6.9 3.2 5.7 2.3
121 5.6 2.8 4.9 2.0
122 7.7 2.8 6.7 2.0
123 6.3 2.7 4.9 1.8
124 6.7 3.3 5.7 2.1
125 7.2 3.2 6.0 1.8
126 6.2 2.8 4.8 1.8
127 6.1 3.0 4.9 1.8
128 6.4 2.8 5.6 2.1
129 7.2 3.0 5.8 1.6
130 7.4 2.8 6.1 1.9
131 7.9 3.8 6.4 2.0
132 6.4 2.8 5.6 2.2
133 6.3 2.8 5.1 1.5
134 6.1 2.6 5.6 1.4
135 7.7 3.0 6.1 2.3
136 6.3 3.4 5.6 2.4
137 6.4 3.1 5.5 1.8
138 6.0 3.0 4.8 1.8
139 6.9 3.1 5.4 2.1
140 6.7 3.1 5.6 2.4
141 6.9 3.1 5.1 2.3
142 5.8 2.7 5.1 1.9
143 6.8 3.2 5.9 2.3
144 6.7 3.3 5.7 2.5
145 6.7 3.0 5.2 2.3
146 6.3 2.5 5.0 1.9
147 6.5 3.0 5.2 2.0
148 6.2 3.4 5.4 2.3
149 5.9 3.0 5.1 1.8
[150 rows x 4 columns]
数据的集中趋势
pandas可以显示关于数据的简短统计摘要
df.mean():返回所有列的均值 df.corr():返回列与列之间的相关系数 df.count():返回每一列中的非空值的个数 df.max():返回每一列的最大值 df.min():返回每一列的最小值 df.median():返回每一列的中位数 df.std():返回每一列的标准差
iris_desc = df_iris.describe()
print(iris_desc)
sepal_length sepa_width peta_length peta_width
count 150.000000 150.000000 150.000000 150.000000
mean 5.843333 3.054000 3.758667 1.198667
std 0.828066 0.433594 1.764420 0.763161
min 4.300000 2.000000 1.000000 0.100000
25% 5.100000 2.800000 1.600000 0.300000
50% 5.800000 3.000000 4.350000 1.300000
75% 6.400000 3.300000 5.100000 1.800000
max 7.900000 4.400000 6.900000 2.500000
#中位数
df_iris.median()
sepal_length 5.80
sepa_width 3.00
peta_length 4.35
peta_width 1.30
dtype: float64
#返回众数
df_iris.mode()
| sepal_length | sepa_width | peta_length | peta_width | |
|---|---|---|---|---|
| 0 | 5.0 | 3.0 | 1.5 | 0.2 |
数据的离中趋势
#方差
df_iris.var()
sepal_length 0.685694
sepa_width 0.188004
peta_length 3.113179
peta_width 0.582414
dtype: float64
#标准差
df_iris.std()
sepal_length 0.828066
sepa_width 0.433594
peta_length 1.764420
peta_width 0.763161
dtype: float64
#极差
df_iris.max()-df_iris.min()
sepal_length 3.6
sepa_width 2.4
peta_length 5.9
peta_width 2.4
dtype: float64
#平均偏差
df_iris.mad()
sepal_length 0.687556
sepa_width 0.333093
peta_length 1.561920
peta_width 0.658933
dtype: float64
#变异系数
df_iris.std()/df_iris.mean()
sepal_length 0.141711
sepa_width 0.141976
peta_length 0.469427
peta_width 0.636675
dtype: float64
#四分位差
iris_desc.loc['75%']-iris_desc.loc['25%']
sepal_length 1.3
sepa_width 0.5
peta_length 3.5
peta_width 1.5
dtype: float64
#偏态(偏度)系数(skew):
iris_desc.skew()
sepal_length 2.819292
sepa_width 2.825707
peta_length 2.820098
peta_width 2.827191
dtype: float64
#峰态系数
iris_desc.kurt()
sepal_length 7.961842
sepa_width 7.988527
peta_length 7.964526
peta_width 7.994746
dtype: float64