pandas 是python的非常好用的数据分析的库,
pandas的优势 增强图表可读性 便捷的数据处理能力 读取文件方便 封装了Matplotlib、Numpy的画图和计算
series
series的创建
Series是一个类似于一维数组的数据结构,它能够保存任何类型的数据,比如整数、字符串、浮点数等,主要由一组数据和与之相关的索引两部分构成。
import pandas as pd
import numpy as np
参数:
data:传入的数据,可以是ndarray、list等
index:索引,必须是唯一的,且与数据的长度相等。如果没有传入索引参数,则默认会自动创建一个从0-N的整数索引。
dtype:数据的类型
pd. Series( np. arange( 9 ) )
0 0
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
dtype: int32
pd. Series( [ 1.2 , 2.3 , 4.5 , 5.0 ] , index= [ 1 , 2 , 3 , 4 ] )
1 1.2
2 2.3
3 4.5
4 5.0
dtype: float64
color_count = pd. Series( {
"red" : 10 , "green" : 20 , "blue" : 100 } )
color_count
red 10
green 20
blue 100
dtype: int64
series的属性
为了更方便地操作Series对象中的索引和数据,Series中提供了两个属性index和values
color_count. index
Index(['red', 'green', 'blue'], dtype='object')
color_count. values
array([ 10, 20, 100], dtype=int64)
color_count[ 0 ]
10
color_count[ 1 ]
20
DataFrame
DataFrame创建
pd. DataFrame( np. random. randn( 2 , 3 ) )
0
1
2
0
-1.130305
-0.276684
-1.390081
1
-0.096309
1.450461
1.523984
score = np. random. randint( 40 , 100 , ( 10 , 5 ) )
score
array([[47, 80, 91, 99, 80],
[48, 79, 94, 94, 50],
[76, 66, 88, 81, 64],
[62, 61, 90, 97, 83],
[49, 98, 81, 69, 52],
[77, 70, 75, 97, 56],
[97, 67, 45, 40, 49],
[78, 99, 64, 69, 98],
[93, 58, 50, 46, 99],
[49, 70, 64, 68, 72]])
score_df = pd. DataFrame( score)
score_df
0
1
2
3
4
0
47
80
91
99
80
1
48
79
94
94
50
2
76
66
88
81
64
3
62
61
90
97
83
4
49
98
81
69
52
5
77
70
75
97
56
6
97
67
45
40
49
7
78
99
64
69
98
8
93
58
50
46
99
9
49
70
64
68
72
subjects = [ "语文" , "数学" , "英语" , "政治" , "体育" ]
stu = [ "同学" + str ( i) for i in range ( score_df. shape[ 0 ] ) ]
data = pd. DataFrame( score, columns= subjects, index= stu)
stu
['同学0', '同学1', '同学2', '同学3', '同学4', '同学5', '同学6', '同学7', '同学8', '同学9']
data
语文
数学
英语
政治
体育
同学0
47
80
91
99
80
同学1
48
79
94
94
50
同学2
76
66
88
81
64
同学3
62
61
90
97
83
同学4
49
98
81
69
52
同学5
77
70
75
97
56
同学6
97
67
45
40
49
同学7
78
99
64
69
98
同学8
93
58
50
46
99
同学9
49
70
64
68
72
DataFrame的属性
data. shape
(10, 5)
data. index
Index(['同学0', '同学1', '同学2', '同学3', '同学4', '同学5', '同学6', '同学7', '同学8', '同学9'], dtype='object')
data. columns
Index(['语文', '数学', '英语', '政治', '体育'], dtype='object')
data. values
array([[47, 80, 91, 99, 80],
[48, 79, 94, 94, 50],
[76, 66, 88, 81, 64],
[62, 61, 90, 97, 83],
[49, 98, 81, 69, 52],
[77, 70, 75, 97, 56],
[97, 67, 45, 40, 49],
[78, 99, 64, 69, 98],
[93, 58, 50, 46, 99],
[49, 70, 64, 68, 72]])
data. T
同学0
同学1
同学2
同学3
同学4
同学5
同学6
同学7
同学8
同学9
语文
47
48
76
62
49
77
97
78
93
49
数学
80
79
66
61
98
70
67
99
58
70
英语
91
94
88
90
81
75
45
64
50
64
政治
99
94
81
97
69
97
40
69
46
68
体育
80
50
64
83
52
56
49
98
99
72
data
语文
数学
英语
政治
体育
同学0
47
80
91
99
80
同学1
48
79
94
94
50
同学2
76
66
88
81
64
同学3
62
61
90
97
83
同学4
49
98
81
69
52
同学5
77
70
75
97
56
同学6
97
67
45
40
49
同学7
78
99
64
69
98
同学8
93
58
50
46
99
同学9
49
70
64
68
72
data. head( )
语文
数学
英语
政治
体育
同学0
47
80
91
99
80
同学1
48
79
94
94
50
同学2
76
66
88
81
64
同学3
62
61
90
97
83
同学4
49
98
81
69
52
data. head( 3 )
语文
数学
英语
政治
体育
同学0
47
80
91
99
80
同学1
48
79
94
94
50
同学2
76
66
88
81
64
data. tail( 3 )
语文
数学
英语
政治
体育
同学7
78
99
64
69
98
同学8
93
58
50
46
99
同学9
49
70
64
68
72
DataFrame索引值的设置
stu = [ "同学_" + str ( i) for i in range ( score_df. shape[ 0 ] ) ]
data. index = stu
data
语文
数学
英语
政治
体育
同学_0
47
80
91
99
80
同学_1
48
79
94
94
50
同学_2
76
66
88
81
64
同学_3
62
61
90
97
83
同学_4
49
98
81
69
52
同学_5
77
70
75
97
56
同学_6
97
67
45
40
49
同学_7
78
99
64
69
98
同学_8
93
58
50
46
99
同学_9
49
70
64
68
72
data. reset_index( )
index
语文
数学
英语
政治
体育
0
同学_0
47
80
91
99
80
1
同学_1
48
79
94
94
50
2
同学_2
76
66
88
81
64
3
同学_3
62
61
90
97
83
4
同学_4
49
98
81
69
52
5
同学_5
77
70
75
97
56
6
同学_6
97
67
45
40
49
7
同学_7
78
99
64
69
98
8
同学_8
93
58
50
46
99
9
同学_9
49
70
64
68
72
data. reset_index( drop= True )
语文
数学
英语
政治
体育
0
47
80
91
99
80
1
48
79
94
94
50
2
76
66
88
81
64
3
62
61
90
97
83
4
49
98
81
69
52
5
77
70
75
97
56
6
97
67
45
40
49
7
78
99
64
69
98
8
93
58
50
46
99
9
49
70
64
68
72
df = pd. DataFrame( {
'month' : [ 1 , 4 , 7 , 10 ] ,
'year' : [ 2012 , 2014 , 2013 , 2014 ] ,
'sale' : [ 55 , 40 , 84 , 31 ] } )
df
month
year
sale
0
1
2012
55
1
4
2014
40
2
7
2013
84
3
10
2014
31
df. set_index( "year" )
month
sale
year
2012
1
55
2014
4
40
2013
7
84
2014
10
31
df = df. set_index( [ "year" , "month" ] )
df
sale
year
month
2012
1
55
2014
4
40
2013
7
84
2014
10
31
MultiIndex与Panel
MultiIndex
df
sale
year
month
2012
1
55
2014
4
40
2013
7
84
2014
10
31
df. index
MultiIndex([(2012, 1),
(2014, 4),
(2013, 7),
(2014, 10)],
names=['year', 'month'])
df. index. names
FrozenList(['year', 'month'])
df. index. levels
FrozenList([[2012, 2013, 2014], [1, 4, 7, 10]])
arrays = [ [ 1 , 1 , 2 , 2 ] , [ "r" , "b" , "r" , "b" ] ]
pd. MultiIndex. from_arrays( arrays, names= ( "num" , "col" ) )
MultiIndex([(1, 'r'),
(1, 'b'),
(2, 'r'),
(2, 'b')],
names=['num', 'col'])
panel
p = pd. Panel( data= np. arange( 24 ) . reshape( 4 , 3 , 2 ) ,
items= list ( 'ABCD' ) ,
major_axis= pd. date_range( '20130101' , periods= 3 ) ,
minor_axis= [ 'first' , 'second' ] )
c:\python\lib\site-packages\ipykernel_launcher.py:1: FutureWarning: The Panel class is removed from pandas. Accessing it from the top-level namespace will also be removed in the next version
"""Entry point for launching an IPython kernel.
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-43-78e9fbdccf04> in <module>
2 items=list('ABCD'),
3 major_axis=pd.date_range('20130101', periods=3),
----> 4 minor_axis=['first', 'second'])
TypeError: Panel() takes no arguments
p
p[ : , : , "second" ]
p[ : , : , "first" ]
p[ "A" , : , : ]