Pandas


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import pandas as pd  # 导入pandas库
import numpy as np

pd.Series([1,3,6,np.nan,44,1]) #自动创建0~n-1索引,对应所给的数据

dates = pd.date_range('20160101',periods=6) # 创建一个时间范围的数据

a = np.arange(24).reshape(6,4)
df = pd.DataFrame(a,index=[1,2,3,4,5,6],columns=['a','b','c','d']) #创一个表,内容为一个数组,索引号为时间,栏也为自定义

df['b'] # 取出栏目号为b的对应的索引号和值

df = pd.DataFrame(np.arange(12).reshape((3,4))) #索引号和栏目都从0开始排

df2.dtypes # 查看各栏目类型

df2.index # 查看索引内容

df2.columns # 查看栏目内容

df2.values  # 内容转数组

print(df.sort_values(by=1, ascending=True))

df.sort_index(axis=0, ascending=False) #axis 0——index排序 1——columns排序 ascending False——降序 True——升序

print df[0:3] # 打印跨越多行(0 1 2 三行)

print(df.loc[1]) # 分行打印行号index为1的所有列内容

print df.loc[:,['c','d']] # 打印所有行,列为c、d的内容

df.iloc[5,3] # 第六行第四列的值 从0开始

df.iloc[0:1,1:3] # 行列分别取,注意减1

df.iloc[[1],1:3] # 行取对应行号,从0开始,列取范围注意减1

df.ix[:3,['a','d']] # 前三行,a d两列

print df[df.a%2==1] #打印满座条件的行列

df.B[df.A>4] = 0 # 修改B列中,并且A列中的值大于4的值为0

df['F'] = np.nan # 加上一列数,值为NaN

df.dropna( # 去掉有 NaN 的行或列,
axis=0, # 0: 对行进行操作; 1: 对列进行操作
how='any' # 'any': 只要存在NaN删 'all': 必须全部是NaN才删
) # 注意 这个是不修改原始数据的 要保存得新建变量存储


print df.fillna('A') # 填充NaN 为具体的值 注意 这个是不修改原始数据的

df.isnull() # 判断是否有缺失数据 NaN, 为 True 表示缺失数据:

np.any(df.isnull()) == True # 检测在数据中是否存在 NaN, 如果存在就返回 True:

data = pd.read_csv('students.csv') # 读取csv

data.to_pickle('student.pickle')

res = pd.concat([df1, df2, df3], axis=0) # 上下合并,不重置index

res = pd.concat([df1, df2, df3], axis=0, ignore_index=True) # 上下合并,不重置index

res = pd.concat([df1, df2], axis=0, join='outer') # 上下合并,不写jion默认是 outer 会加上不重复的列,并填充NaN

res = pd.concat([df1, df2], axis=0, join='inner') # 为inner时,只合并共同列

res = pd.concat([df1, df2], axis=1, join_axes=[df1.index]) # 按照df1的index 进行横向合并 只合并df1中的index df2中的忽略

res = pd.concat([df1, df2], axis=1) # 相互合并

res = df1.append([df2, df3], ignore_index=True) # 纵向增加数据

res = pd.merge(left, right, on='key') # 在key——column的基准下合并

res = pd.merge(left, right, on=['key1', 'key2'], how='inner') # 只考虑key1,key2完全一致才合并(inner——默认),其他被忽略

res = pd.merge(left, right, on=['key1', 'key2'], how='outer') # 全合并,无数据的补成NaN

res = pd.merge(a, b, on=['key1', 'key2'], how='left') # 基于左边一个也就是a的key1/2进行合并,没有的补NaN

res = pd.merge(a, b, on=['key1', 'key2'], how='right') # 基于左边一个也就是b的key1/2进行合并,没有的补NaN

res = pd.merge(df1, df2, on='col1', how='outer', indicator=True) # 和之前的合并方式一致,不过会加上指示器,告知数据的合并方式,left_only/right_only/both 参数设置为 indicator='indicator_column' 可以改变栏目名字

res = pd.merge(left, right, left_index=True, right_index=True, how='inner') # 考虑index的合并方式,同样有四种how方式

res = pd.merge(a, b, on='k', suffixes=['_a', '_b'], how='inner') # 重复数据加入后缀主动区分

import matplotlib.pyplot as plt # 导入数据显示模块
data.plot() # 数据绘制
data.cumsum() # 累加求和
plt.show() # 数据渲染

ax = data.plot.scatter(x='A',y='B',color='DarkBlue',label='Class1',ax=ax) # 绘制散点图,根据data中的AB列对应的数据,可以设置颜色和标签,ax=ax可以设置后置的数据绘制,覆盖在前面的