Kernel: Python 3
深入浅出pandas-1
pandas是一个支持数据分析全流程的Python开源库,它的作者Wes McKinney于2008年开始开发这个库,其主要目标是提供一个大数据分析和处理的工具。pandas封装了从数据加载、数据重塑、数据清洗到数据透视、数据呈现等一系列操作,提供了三种核心的数据类型:
Series:数据系列,表示一维的数据。跟一维数组的区别在于每条数据都有对应的索引,处理数据的方法比ndarray更为丰富。DataFrame:数据框、数据窗、数据表,表示二维的数据。跟二维数组相比,DataFrame有行索引和列索引,而且提供了100+方法来处理数据。Index:为Series和DataFrame提供索引服务。
In [1]:
创建DataFrame对象
In [2]:
In [3]:
Out[3]:
array([[ 95, 86, 75],
[ 91, 88, 86],
[ 69, 80, 71],
[ 82, 67, 94],
[ 92, 100, 81]])
In [4]:
Out[4]:
In [5]:
Out[5]:
Index(['狄仁杰', '白起', '李元芳', '苏妲己', '孙尚香'], dtype='object')
In [6]:
Out[6]:
Index(['语文', '数学', '英语'], dtype='object')
In [7]:
Out[7]:
array([[ 95, 86, 75],
[ 91, 88, 86],
[ 69, 80, 71],
[ 82, 67, 94],
[ 92, 100, 81]])
In [8]:
In [9]:
Out[9]:
In [10]:
Out[10]:
<class 'pandas.core.frame.DataFrame'>
Index: 5 entries, 狄仁杰 to 孙尚香
Data columns (total 3 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 语文 5 non-null int64
1 数学 5 non-null int64
2 英语 5 non-null int64
dtypes: int64(3)
memory usage: 558.0 bytes
In [11]:
Out[11]:
In [12]:
In [13]:
Out[13]:
In [14]:
In [15]:
Out[15]:
Engine(mysql+pymysql://guest:***@47.109.26.237:3306/hrs)
In [16]:
Out[16]:
In [17]:
Out[17]:
In [18]:
Out[18]:
In [19]:
查看信息
In [20]:
Out[20]:
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 1945 entries, 0 to 1944
Data columns (total 8 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 销售日期 1945 non-null datetime64[ns]
1 销售区域 1945 non-null object
2 销售渠道 1945 non-null object
3 销售订单 1945 non-null object
4 品牌 1945 non-null object
5 售价 1945 non-null int64
6 销售数量 1945 non-null int64
7 直接成本 1945 non-null int64
dtypes: datetime64[ns](1), int64(3), object(4)
memory usage: 121.7+ KB
In [21]:
Out[21]:
In [22]:
Out[22]:
操作行、列、单元格
In [23]:
Out[23]:
0 2020-01-01
1 2020-01-01
2 2020-01-01
3 2020-01-01
4 2020-01-01
...
1940 2020-12-30
1941 2020-12-30
1942 2020-12-31
1943 2020-12-31
1944 2020-12-31
Name: 销售日期, Length: 1945, dtype: datetime64[ns]
In [24]:
Out[24]:
0 拼多多
1 抖音
2 天猫
3 天猫
4 天猫
...
1940 京东
1941 实体
1942 实体
1943 抖音
1944 天猫
Name: 销售渠道, Length: 1945, dtype: object
In [25]:
Out[25]:
0 拼多多
1 抖音
2 天猫
3 天猫
4 天猫
...
1940 京东
1941 实体
1942 实体
1943 抖音
1944 天猫
Name: 销售渠道, Length: 1945, dtype: object
In [26]:
Out[26]:
pandas.core.series.Series
In [27]:
Out[27]:
In [28]:
Out[28]:
pandas.core.frame.DataFrame
In [29]:
Out[29]:
In [30]:
Out[30]:
Index(['销售订单', '品牌', '售价', '销售数量'], dtype='object')
In [31]:
Out[31]:
In [32]:
Out[32]:
销售日期 2020-12-31 00:00:00
销售区域 福建
销售渠道 天猫
销售订单 211807-050
品牌 八匹马
售价 99
销售数量 27
直接成本 435
Name: 1944, dtype: object
In [33]:
Out[33]:
销售日期 2020-12-31 00:00:00
销售区域 福建
销售渠道 天猫
销售订单 211807-050
品牌 八匹马
售价 99
销售数量 27
直接成本 435
Name: 1944, dtype: object
In [34]:
Out[34]:
In [35]:
Out[35]:
In [36]:
Out[36]:
In [37]:
Out[37]:
In [38]:
Out[38]:
169
In [39]:
Out[39]:
In [40]:
Out[40]:
In [41]:
Out[41]:
In [42]:
In [43]:
Out[43]:
In [44]:
Out[44]:
In [45]:
Out[45]:
In [46]:
Out[46]:
In [47]:
Out[47]:
In [48]:
Out[48]:
In [49]:
Out[49]:
In [50]:
Out[50]:
In [51]:
Out[51]:
In [52]:
Out[52]:
In [53]:
Out[53]:
In [54]:
Out[54]:
In [55]:
Out[55]:
In [56]:
Out[56]:
In [57]:
Out[57]:
数据重塑
拼接(合并结构一致的数据)
合并(事实表连接维度表)
In [58]:
Out[58]:
(19, 6)
In [59]:
Out[59]:
In [60]:
数据清洗
缺失值
重复值
异常值
预处理
In [61]:
Out[61]:
In [62]:
Out[62]:
0 False
1 False
2 False
3 True
4 True
5 True
6 True
7 False
8 True
9 True
10 True
11 True
12 False
13 False
14 False
15 False
16 False
17 False
18 False
Name: comm, dtype: bool
In [63]:
Out[63]:
0 True
1 True
2 True
3 False
4 False
5 False
6 False
7 True
8 False
9 False
10 False
11 False
12 True
13 True
14 True
15 True
16 True
17 True
18 True
Name: comm, dtype: bool
In [64]:
Out[64]:
comm
True 11
False 8
Name: count, dtype: int64
In [65]:
Out[65]:
In [66]:
Out[66]:
In [67]:
Out[67]:
0 3344.0
1 7800.0
2 2056.0
3 2056.0
4 2056.0
5 3088.0
6 2056.0
7 7800.0
8 5566.0
9 5566.0
10 3344.0
11 5566.0
12 7800.0
15 9800.0
17 7800.0
18 9800.0
Name: mgr, dtype: float64
In [68]:
Out[68]:
In [69]:
Out[69]:
0 200
1 1500
2 800
3 0
4 0
5 0
6 0
7 800
8 0
9 0
10 0
11 0
12 1000
13 1200
14 8000
15 600
16 6000
17 5000
18 1200
Name: comm, dtype: int64
In [70]:
Out[70]:
0 200.0
1 1500.0
2 800.0
3 800.0
4 800.0
5 800.0
6 800.0
7 800.0
8 1000.0
9 1000.0
10 1000.0
11 1000.0
12 1000.0
13 1200.0
14 8000.0
15 600.0
16 6000.0
17 5000.0
18 1200.0
Name: comm, dtype: float64
In [71]:
Out[71]:
0 200.0
1 1500.0
2 800.0
3 800.0
4 800.0
5 800.0
6 800.0
7 800.0
8 800.0
9 800.0
10 800.0
11 800.0
12 1000.0
13 1200.0
14 8000.0
15 600.0
16 6000.0
17 5000.0
18 1200.0
Name: comm, dtype: float64
In [72]:
In [73]:
Out[73]:
In [74]:
Out[74]:
In [75]:
Out[75]:
0 False
1 False
2 False
3 False
4 False
5 False
6 False
7 False
8 False
9 False
10 False
11 False
12 False
13 False
14 True
15 False
16 True
17 False
18 False
Name: ename, dtype: bool
In [76]:
Out[76]:
0 False
1 False
2 False
3 False
4 False
5 False
6 False
7 False
8 False
9 False
10 False
11 False
12 False
13 False
14 True
15 False
16 True
17 False
18 False
dtype: bool
In [77]:
Out[77]:
ename
张三丰 3
胡一刀 1
朱九真 1
骆昊 1
王大锤 1
宋远桥 1
郭靖 1
苗人凤 1
杨过 1
乔峰 1
黄蓉 1
张翠山 1
欧阳锋 1
丘处机 1
张无忌 1
李莫愁 1
陈小刀 1
Name: count, dtype: int64
In [78]:
Out[78]:
job
程序员 5
总裁 3
销售员 2
分析师 2
会计 2
设计师 1
销售主管 1
出纳 1
会计师 1
架构师 1
Name: count, dtype: int64
In [79]:
Out[79]:
17
In [80]:
Out[80]:
In [81]:
In [82]:
Out[82]:
array([ 83., 81., 89., 89., 76., 79., 78., 76., 79., 74., 89.,
61., 90., 74., 68., 81., 81., 93., 69., 81., 76., 87.,
80., 90., 72., 89., 72., 71., 93., 75., 75., 73., 85.,
91., 96., 82., 74., 80., 72., 83., 72., 64., 83., 79.,
78., 68., 68., 70., 68., 84., 120., 160., 200., 40., 20.,
-50.])
In [83]:
Out[83]:
array([120., 160., 200., 40., 20., -50.])
In [84]:
Out[84]:
array([160., 200., 20., -50.])
In [85]:
In [86]:
Out[86]:
array([200., -50.])
In [87]:
Out[87]:
array([160., 200., 20., -50.])
In [88]:
Out[88]:
订单号
G69924 23688
G70509 31935
G72204 26758
G70509 31594
G72186 30583
G70509 52302
G69631 32125
543369-010 29843
543367-077 31889
G69627 31028
G69645 23947
G72201 40327
G69631 26534
543367-077 25674
G71332 35120
588705-010 25502
543367-077 31375
G68188 29819
577714-010 40884
FT001-18-1763 25835
G72186 24770
G71330 29795
577714-010 27244
FT007-18-1763 25454
G69627 24634
G69627 23537
G72204 31613
543367-077 45442
G85411 22861
G68188 34290
AYMH063-1 29307
G69645 37782
Name: 直接成本, dtype: int64
In [89]:
Out[89]:
In [90]:
Out[90]:
79.0
In [91]:
Out[91]:
array([160., 200., 20., -50.])
In [92]:
Out[92]:
array([False, False, False, False, False, False, False, False, False,
False, False, False, False, False, False, False, False, False,
False, False, False, False, False, False, False, False, False,
False, False, False, False, False, False, False, False, False,
False, False, False, False, False, False, False, False, False,
False, False, False, False, False, False, True, True, False,
True, True])
In [93]:
In [94]:
Out[94]:
array([ 83., 81., 89., 89., 76., 79., 78., 76., 79., 74., 89.,
61., 90., 74., 68., 81., 81., 93., 69., 81., 76., 87.,
80., 90., 72., 89., 72., 71., 93., 75., 75., 73., 85.,
91., 96., 82., 74., 80., 72., 83., 72., 64., 83., 79.,
78., 68., 68., 70., 68., 84., 120., 79., 79., 40., 79.,
79.])
案例1:招聘数据清洗和预处理
数据加载
去重
数据抽取
拆分列
替换值
数据筛选
In [95]:
Out[95]:
In [96]:
Out[96]:
(9777, 9)
In [97]:
Out[97]:
In [98]:
Out[98]:
In [99]:
Out[99]:
In [100]:
In [101]:
Out[101]:
array(['1-3年', '3-5年', '1年以内', '经验不限', '5年以上', '应届生'], dtype=object)
In [102]:
In [103]:
Out[103]:
array(['本科', '大专', '学历不限', '研究生'], dtype=object)
In [104]:
Out[104]:
(6487, 9)
In [105]:
案例2:北京积分落户数据预处理
加载数据
日期时间处理
年龄段分箱
落户积分归一化
In [106]:
Out[106]:
In [107]:
Out[107]:
<class 'pandas.core.frame.DataFrame'>
Index: 6003 entries, 202300001 to 202306003
Data columns (total 4 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 姓名 6003 non-null object
1 出生年月 6003 non-null object
2 单位名称 6003 non-null object
3 积分分值 6003 non-null float64
dtypes: float64(1), object(3)
memory usage: 234.5+ KB
In [108]:
Out[108]:
<class 'pandas.core.frame.DataFrame'>
Index: 6003 entries, 202300001 to 202306003
Data columns (total 4 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 姓名 6003 non-null object
1 出生年月 6003 non-null datetime64[ns]
2 单位名称 6003 non-null object
3 积分分值 6003 non-null float64
dtypes: datetime64[ns](1), float64(1), object(2)
memory usage: 234.5+ KB
In [109]:
Out[109]:
In [110]:
Out[110]:
In [111]:
Out[111]:
年龄段
40~44岁 4215
45~49岁 1053
35~39岁 681
50~54岁 34
55~59岁 20
Name: count, dtype: int64
In [112]:
Out[112]:
array([[0. , 0.39277201, 0.15816993, 1. ],
[0.18246828, 0.59332564, 0.30675894, 1. ],
[0.45176471, 0.76708958, 0.46120723, 1. ],
[0.72312188, 0.88961169, 0.69717801, 1. ],
[0.91326413, 0.96670511, 0.89619377, 1. ]])
In [113]:
Out[113]:
In [114]:
Out[114]:
In [115]:
Out[115]:
mean 111.849640
max 140.050000
min 109.920000
std 2.481941
skew 3.485351
kurt 17.390027
Name: 积分分值, dtype: float64
线性归一化:
In [116]:
Out[116]:
(140.05, 109.92)
In [117]:
Out[117]:
zscore标准化:
In [118]:
Out[118]: