pandas在python中的使用:
在python中默认用 import pandas as pd 导入pandas库,你可以用 pd.__version__ 查看你安装的版本。
pandas中主要有两种数据结构:Series 和 DataFrame。下面我们将介绍 Series 。
Series:一种类似于一维数组的对象,是由一组数据(一种NumPy数据类型)以及一组与之相关的数据标签(即索引)
组成。仅有一组数据也可以产生简单的Series对象。注意:Series中的索引值是可以重复的。
DataFrame:一个表格型的数据结构,包含有一组有序的列,每列可以是不同的值类型(数值,字符串,布尔型等),
DataFrame 即有行索引也有列索引,可以被看做是由Series组成的字典。
上面的表诉来源于博客:pandas---Series基础使用 。
Series对象:
生成Series对象
1 import pandas as pd 2 # print(pd.__version__)
3 s = pd.Series([True, 1, 2, 'kl']) # 默认添加index
4 print("python%s的数据:\n" % type(s), s) 5 Out[1]: 6 python<class 'pandas.core.series.Series'>的数据: 7 0 True 8 1 1
9 2 2
10 3 kl 11 dtype: object
为Series对象添加index
1 s = pd.Series([True, 1, 2, 'kl'], index=['logical', 'num1', 'num2', 'id']) 2 print("python%s的数据:\n" % type(s), s) 3 Out[2]: 4 python<class 'pandas.core.series.Series'>的数据: 5 logical True 6 num1 1
7 num2 2
8 id kl 9 dtype: object
获取元素
1 print(s.values) 2 print(s.index) 3 print(s[2]) 4 print(s['num1']) 5 print(s[:2]) 6 print(s[['num1', 'num2']]) 7 Out[3]: 8 [True 1 2 'kl'] 9 Index(['logical', 'num1', 'num2', 'id'], dtype='object') 10 2
11 1
12 logical True 13 num1 1
14 dtype: object 15 num1 1
16 num2 2
17 dtype: object
为元素赋值
1 s = pd.Series([True, 1, 2, 'kl'], index=['logical', 'num1', 'num2', 'id']) 2 s[0] = False 3 s['num1'] = 1.1
4 print(s) 5 Out[4]: 6 logical False 7 num1 1.1
8 num2 2
9 id kl 10 dtype: object
用Numpy数组或其他Series对象定义新Series对象
1 a = np.array([1, 2, 3, 4]) 2 s1 = pd.Series(a) 3 s2 = pd.Series(s1) 4 print("s1:\n", s1) 5 print("s2:\n", s2) 6 print(s1 == s2) 7 s1[2] = 100
8 print("s1更改后的s2:\n", s2) 9 Out[5]: 10 s1: 11 0 1
12 1 2
13 2 3
14 3 4
15 dtype: int32 16 s2: 17 0 1
18 1 2
19 2 3
20 3 4
21 dtype: int32 22 0 True 23 1 True 24 2 True 25 3 True 26 dtype: bool 27 s1更改后的s2: 28 0 1
29 1 2
30 2 100
31 3 4
32 dtype: int32
注意上面的s1更改之后,s2也发生了相应的变化,对比深复制与浅复制。
筛选元素
1 a = pd.Series(np.array([1, 2, 3, 4]))
2 print(a[a < 3])
3 Out[6]:
4 [1 2]
运算和数学函数
1 s1 = pd.Series([6, 1, 2, 9]) # 可以加减乘除
2 b = s1 + 2
3 print(b) 4 print(np.log(s1)) 5 Out[7]: 6 0 8
7 1 3
8 2 4
9 3 11
10 dtype: int64 11 0 1.791759
12 1 0.000000
13 2 0.693147
14 3 2.197225
15 dtype: float64
Series对象的组成元素
1 color = pd.Series([1, 0, 2, 1, 2, 3], index=['white', 'white', 'blue', 'green', 'green', 'yellow']) 2 print("color:\n", color) 3 print("color.unique():\n", color.unique()) 4 print("color.value_counts():\n", color.value_counts()) 5 print("color.isin():\n", color.isin([0, 3])) 6 print("color[color.isin([0, 3])]:\n", color[color.isin([0, 3])]) 7 Out[8]: 8 color: 9 white 1
10 white 0 11 blue 2
12 green 1
13 green 2
14 yellow 3
15 dtype: int64 16 color.unique(): 17 [1 0 2 3] 18 color.value_counts(): 19 2 2
20 1 2
21 3 1
22 0 1
23 dtype: int64 24 color.isin(): 25 white False 26 white True 27 blue False 28 green False 29 green False 30 yellow True 31 dtype: bool 32 color[color.isin([0, 3])]: 33 white 0 34 yellow 3
35 dtype: int64
缺失值NaN
1 s = pd.Series([1, 2, np.nan, 6]) 2 print(s.isnull()) 3 print(s.notnull()) 4 print(s[s.notnull()]) 5 Out[9]: 6 0 False 7 1 False 8 2 True 9 3 False 10 dtype: bool 11 0 True 12 1 True 13 2 False 14 3 True 15 dtype: bool 16 0 1.0
17 1 2.0
18 3 6.0
19 dtype: float64
Series用作字典
1 mydict = {'red':200, 'blue':100, 'yellow':50, 'orange':100} 2 myseries = pd.Series(mydict) 3 print(myseries) 4 Out[10]: 5 red 200
6 blue 100
7 yellow 50
8 orange 100
9 dtype: int64
当然你也可以用index参数指定index。
Series 对象之间的运算
1 mydict0 = {'red':200, 'blue':100, 'yellow':50, 'orange':100} 2 myseries0 = pd.Series(mydict0) 3 print(myseries0) 4 mydict1 = {'red':200, 'blue':100, 'yellow':50, 'orange':100, 'black':30} 5 myseries1 = pd.Series(mydict1) 6 print(myseries0 + myseries1) 7 Out[11]: 8 red 200
9 blue 100
10 yellow 50
11 orange 100
12 dtype: int64 13 black NaN 14 blue 200.0
15 orange 200.0
16 red 400.0
17 yellow 100.0
18 dtype: float64
注意 myseries0 是没有black的 所以相加时默认以NaN补位。