首页 > 后端开发 > Python教程 > pandas 基础

pandas 基础

巴扎黑
发布: 2017-06-23 15:54:39
原创
2522 人浏览过

pandas 是基于 Numpy 构建的含有更高级数据结构和工具的数据分析包

类似于 Numpy 的核心是 ndarray,pandas 也是围绕着 Series 和 DataFrame 两个核心数据结构展开的 。Series 和 DataFrame 分别对应于一维的序列和二维的表结构。pandas 约定俗成的导入方法如下:

1

2

from pandas import Series,DataFrame

import pandas as pd

登录后复制

Series


Series 可以看做一个定长的有序字典。基本任意的一维数据都可以用来构造 Series 对象:

1

2

3

4

5

6

7

>>> s = Series([1,2,3.0,'abc'])

>>> s

0      1

1      2

2      3

3    abc

dtype: object

登录后复制

虽然 dtype:object 可以包含多种基本数据类型,但总感觉会影响性能的样子,最好还是保持单纯的 dtype。

Series 对象包含两个主要的属性:index 和 values,分别为上例中左右两列。因为传给构造器的是一个列表,所以 index 的值是从 0 起递增的整数,如果传入的是一个类字典的键值对结构,就会生成 index-value 对应的 Series;或者在初始化的时候以关键字参数显式指定一个 index 对象:

1

2

3

4

5

6

7

8

9

10

11

>>> s = Series(data=[1,3,5,7],index = ['a','b','x','y'])

>>> s

a    1

b    3

x    5

y    7

dtype: int64

>>> s.index

Index(['a', 'b', 'x', 'y'], dtype='object')

>>> s.values

array([1, 3, 5, 7], dtype=int64)

登录后复制

Series 对象的元素会严格依照给出的 index 构建,这意味着:如果 data 参数是有键值对的,那么只有 index 中含有的键会被使用;以及如果 data 中缺少响应的键,即使给出 NaN 值,这个键也会被添加。

注意 Series 的 index 和 values 的元素之间虽然存在对应关系,但这与字典的映射不同。index 和 values 实际仍为互相独立的 ndarray 数组,因此 Series 对象的性能完全 ok。

Series 这种使用键值对的数据结构最大的好处在于,Series 间进行算术运算时,index 会自动对齐。

另外,Series 对象和它的 index 都含有一个 name 属性:

1

2

3

4

5

6

7

8

9

>>> s.name = 'a_series'

>>> s.index.name = 'the_index'

>>> s

the_index

a            1

b            3

x            5

y            7

Name: a_series, dtype: int64

登录后复制

DataFrame


DataFrame 是一个表格型的数据结构,它含有一组有序的列(类似于 index),每列可以是不同的值类型(不像 ndarray 只能有一个 dtype)。基本上可以把 DataFrame 看成是共享同一个 index 的 Series 的集合。

DataFrame 的构造方法与 Series 类似,只不过可以同时接受多条一维数据源,每一条都会成为单独的一列:

1

2

3

4

5

6

7

8

9

10

11

12

13

>>> data = {'state':['Ohino','Ohino','Ohino','Nevada','Nevada'],

        'year':[2000,2001,2002,2001,2002],

        'pop':[1.5,1.7,3.6,2.4,2.9]}

>>> df = DataFrame(data)

>>> df

   pop   state  year

0  1.5   Ohino  2000

1  1.7   Ohino  2001

2  3.6   Ohino  2002

3  2.4  Nevada  2001

4  2.9  Nevada  2002

 

[5 rows x 3 columns]

登录后复制

虽然参数 data 看起来是个字典,但字典的键并非充当 DataFrame 的 index 的角色,而是 Series 的 “name” 属性。这里生成的 index 仍是 “01234”。

较完整的 DataFrame 构造器参数为:DataFrame(data=None,index=None,coloumns=None),columns 即 “name”:

1

2

3

4

5

6

7

8

9

10

11

>>> df = DataFrame(data,index=['one','two','three','four','five'],

               columns=['year','state','pop','debt'])

>>> df

       year   state  pop debt

one    2000   Ohino  1.5  NaN

two    2001   Ohino  1.7  NaN

three  2002   Ohino  3.6  NaN

four   2001  Nevada  2.4  NaN

five   2002  Nevada  2.9  NaN

 

[5 rows x 4 columns]

登录后复制

同样缺失值由 NaN 补上。看一下 index、columns 和 索引的类型:

1

2

3

4

5

6

>>> df.index

Index(['one', 'two', 'three', 'four', 'five'], dtype='object')

>>> df.columns

Index(['year', 'state', 'pop', 'debt'], dtype='object')

>>> type(df['debt'])

<class &#39;pandas.core.series.Series&#39;>

登录后复制

DataFrame 面向行和面向列的操作基本是平衡的,任意抽出一列都是 Series。

对象属性


重新索引

Series 对象的重新索引通过其 .reindex(index=None,**kwargs) 方法实现。**kwargs 中常用的参数有俩:method=None,fill_value=np.NaN

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

ser = Series([4.5,7.2,-5.3,3.6],index=[&#39;d&#39;,&#39;b&#39;,&#39;a&#39;,&#39;c&#39;])

>>> a = [&#39;a&#39;,&#39;b&#39;,&#39;c&#39;,&#39;d&#39;,&#39;e&#39;]

>>> ser.reindex(a)

a   -5.3

b    7.2

c    3.6

d    4.5

e    NaN

dtype: float64

>>> ser.reindex(a,fill_value=0)

a   -5.3

b    7.2

c    3.6

d    4.5

e    0.0

dtype: float64

>>> ser.reindex(a,method=&#39;ffill&#39;)

a   -5.3

b    7.2

c    3.6

d    4.5

e    4.5

dtype: float64

>>> ser.reindex(a,fill_value=0,method=&#39;ffill&#39;)

a   -5.3

b    7.2

c    3.6

d    4.5

e    4.5

dtype: float64

登录后复制

.reindex() 方法会返回一个新对象,其 index 严格遵循给出的参数,method:{&#39;backfill&#39;, &#39;bfill&#39;, &#39;pad&#39;, &#39;ffill&#39;, None} 参数用于指定插值(填充)方式,当没有给出时,自动用 fill_value 填充,默认为 NaN(ffill = pad,bfill = back fill,分别指插值时向前还是向后取值)

DataFrame 对象的重新索引方法为:.reindex(index=None,columns=None,**kwargs)。仅比 Series 多了一个可选的 columns 参数,用于给列索引。用法与上例类似,只不过插值方法 method 参数只能应用于行,即轴 0。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

>>> state = [&#39;Texas&#39;,&#39;Utha&#39;,&#39;California&#39;]

>>> df.reindex(columns=state,method=&#39;ffill&#39;)

    Texas  Utha  California

a      1   NaN           2

c      4   NaN           5 

d      7   NaN           8

 

[3 rows x 3 columns]

>>> df.reindex(index=[&#39;a&#39;,&#39;b&#39;,&#39;c&#39;,&#39;d&#39;],columns=state,method=&#39;ffill&#39;)

   Texas  Utha  California

a      1   NaN           2

b      1   NaN           2

c      4   NaN           5

d      7   NaN           8

 

[4 rows x 3 columns]

登录后复制

不过 fill_value 依然对有效。聪明的小伙伴可能已经想到了,可不可以通过 df.T.reindex(index,method=&#39;**&#39;).T 这样的方式来实现在列上的插值呢,答案是可行的。另外要注意,使用 reindex(index,method=&#39;**&#39;) 的时候,index 必须是单调的,否则就会引发一个 ValueError: Must be monotonic for forward fill,比如上例中的最后一次调用,如果使用 index=[&#39;a&#39;,&#39;b&#39;,&#39;d&#39;,&#39;c&#39;] 的话就不行。

删除指定轴上的项

即删除 Series 的元素或 DataFrame 的某一行(列)的意思,通过对象的 .drop(labels, axis=0) 方法:

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

>>> ser

d    4.5

b    7.2

a   -5.3

c    3.6

dtype: float64

>>> df

   Ohio  Texas  California

a     0      1           2

c     3      4           5

d     6      7           8

 

[3 rows x 3 columns]

>>> ser.drop(&#39;c&#39;)

d    4.5

b    7.2

a   -5.3

dtype: float64

>>> df.drop(&#39;a&#39;)

   Ohio  Texas  California

c     3      4           5

d     6      7           8

 

[2 rows x 3 columns]

>>> df.drop([&#39;Ohio&#39;,&#39;Texas&#39;],axis=1)

   California

a           2

c           5

d           8

 

[3 rows x 1 columns]

登录后复制

.drop() 返回的是一个新对象,元对象不会被改变。

索引和切片

就像 Numpy,pandas 也支持通过 obj[::] 的方式进行索引和切片,以及通过布尔型数组进行过滤。

不过须要注意,因为 pandas 对象的 index 不限于整数,所以当使用非整数作为切片索引时,它是末端包含的。

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

>>> foo

a    4.5

b    7.2

c   -5.3

d    3.6

dtype: float64

>>> bar

0    4.5

1    7.2

2   -5.3

3    3.6

dtype: float64

>>> foo[:2]

a    4.5

b    7.2

dtype: float64

>>> bar[:2]

0    4.5

1    7.2

dtype: float64

>>> foo[:&#39;c&#39;]

a    4.5

b    7.2

c   -5.3

dtype: float64

登录后复制

这里 foo 和 bar 只有 index 不同——bar 的 index 是整数序列。可见当使用整数索引切片时,结果与 Python 列表或 Numpy 的默认状况相同;换成 &#39;c&#39; 这样的字符串索引时,结果就包含了这个边界元素。

另外一个特别之处在于 DataFrame 对象的索引方式,因为他有两个轴向(双重索引)。

可以这么理解:DataFrame 对象的标准切片语法为:.ix[::,::]。ix 对象可以接受两套切片,分别为行(axis=0)和列(axis=1)的方向:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

>>> df

   Ohio  Texas  California

a     0      1           2

c     3      4           5

d     6      7           8

 

[3 rows x 3 columns]

>>> df.ix[:2,:2]

   Ohio  Texas

a     0      1

c     3      4

 

[2 rows x 2 columns]

>>> df.ix[&#39;a&#39;,&#39;Ohio&#39;]

0

登录后复制

而不使用 ix ,直接切的情况就特殊了:

  • 索引时,选取的是列

  • 切片时,选取的是行

这看起来有点不合逻辑,但作者解释说 “这种语法设定来源于实践”,我们信他。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

>>> df[&#39;Ohio&#39;]

a    0

c    3

d    6

Name: Ohio, dtype: int32

>>> df[:&#39;c&#39;]

   Ohio  Texas  California

a     0      1           2

c     3      4           5

 

[2 rows x 3 columns]

>>> df[:2]

   Ohio  Texas  California

a     0      1           2

c     3      4           5

 

[2 rows x 3 columns]

登录后复制

使用布尔型数组的情况,注意行与列的不同切法(列切法的 : 不能省):

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

>>> df[&#39;Texas&#39;]>=4

a    False

c     True

d     True

Name: Texas, dtype: bool

>>> df[df[&#39;Texas&#39;]>=4]

   Ohio  Texas  California

c     3      4           5

d     6      7           8

 

[2 rows x 3 columns]

>>> df.ix[:,df.ix[&#39;c&#39;]>=4]

   Texas  California

a      1           2

c      4           5

d      7           8

 

[3 rows x 2 columns]

登录后复制

算术运算和数据对齐

pandas 最重要的一个功能是,它可以对不同索引的对象进行算术运算。在将对象相加时,结果的索引取索引对的并集。自动的数据对齐在不重叠的索引处引入空值,默认为 NaN。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

>>> foo = Series({&#39;a&#39;:1,&#39;b&#39;:2})

>>> foo

a    1

b    2

dtype: int64

>>> bar = Series({&#39;b&#39;:3,&#39;d&#39;:4})

>>> bar

b    3

d    4

dtype: int64

>>> foo + bar

a   NaN

b     5

d   NaN

dtype: float64

登录后复制

DataFrame 的对齐操作会同时发生在行和列上。

当不希望在运算结果中出现 NA 值时,可以使用前面 reindex 中提到过 fill_value 参数,不过为了传递这个参数,就需要使用对象的方法,而不是操作符:df1.add(df2,fill_value=0)。其他算术方法还有:sub(), div(), mul()

Series 和 DataFrame 之间的算术运算涉及广播,暂时先不讲。

函数应用和映射

Numpy 的 ufuncs(元素级数组方法)也可用于操作 pandas 对象。

当希望将函数应用到 DataFrame 对象的某一行或列时,可以使用 .apply(func, axis=0, args=(), **kwds) 方法。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

f = lambda x:x.max()-x.min()

>>> df

   Ohio  Texas  California

a     0      1           2

c     3      4           5

d     6      7           8

 

[3 rows x 3 columns]

>>> df.apply(f)

Ohio          6

Texas         6

California    6

dtype: int64

>>> df.apply(f,axis=1)

a    2

c    2

d    2

dtype: int64

登录后复制

排序和排名

Series 的 sort_index(ascending=True) 方法可以对 index 进行排序操作,ascending 参数用于控制升序或降序,默认为升序。

若要按值对 Series 进行排序,当使用 .order() 方法,任何缺失值默认都会被放到 Series 的末尾。

在 DataFrame 上,.sort_index(axis=0, by=None, ascending=True) 方法多了一个轴向的选择参数与一个 by 参数,by 参数的作用是针对某一(些)列进行排序(不能对行使用 by 参数):

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

>>> df.sort_index(by=&#39;Ohio&#39;)

   Ohio  Texas  California

a     0      1           2

c     3      4           5

d     6      7           8

 

[3 rows x 3 columns]

>>> df.sort_index(by=[&#39;California&#39;,&#39;Texas&#39;])

   Ohio  Texas  California

a     0      1           2

c     3      4           5

d     6      7           8

 

[3 rows x 3 columns]

>>> df.sort_index(axis=1)

   California  Ohio  Texas

a           2     0      1

c           5     3      4

d           8     6      7

 

[3 rows x 3 columns]

登录后复制

排名(Series.rank(method=&#39;average&#39;, ascending=True))的作用与排序的不同之处在于,他会把对象的 values 替换成名次(从 1 到 n)。这时唯一的问题在于如何处理平级项,方法里的 method 参数就是起这个作用的,他有四个值可选:average, min, max, first

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

>>> ser=Series([3,2,0,3],index=list(&#39;abcd&#39;))

>>> ser

a    3

b    2

c    0

d    3

dtype: int64

>>> ser.rank()

a    3.5

b    2.0

c    1.0

d    3.5

dtype: float64

>>> ser.rank(method=&#39;min&#39;)

a    3

b    2

c    1

d    3

dtype: float64

>>> ser.rank(method=&#39;max&#39;)

a    4

b    2

c    1

d    4

dtype: float64

>>> ser.rank(method=&#39;first&#39;)

a    3

b    2

c    1

d    4

dtype: float64

登录后复制

注意在 ser[0]=ser[3] 这对平级项上,不同 method 参数表现出的不同名次。

DataFrame 的 .rank(axis=0, method=&#39;average&#39;, ascending=True) 方法多了个 axis 参数,可选择按行或列分别进行排名,暂时好像没有针对全部元素的排名方法。

统计方法

pandas 对象有一些统计方法。它们大部分都属于约简和汇总统计,用于从 Series 中提取单个值,或从 DataFrame 的行或列中提取一个 Series。

比如 DataFrame.mean(axis=0,skipna=True) 方法,当数据集中存在 NA 值时,这些值会被简单跳过,除非整个切片(行或列)全是 NA,如果不想这样,则可以通过 skipna=False 来禁用此功能:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

>>> df

    one  two

a  1.40  NaN

b  7.10 -4.5

c   NaN  NaN

d  0.75 -1.3

 

[4 rows x 2 columns]

>>> df.mean()

one    3.083333

two   -2.900000

dtype: float64

>>> df.mean(axis=1)

a    1.400

b    1.300

c      NaN

d   -0.275

dtype: float64

>>> df.mean(axis=1,skipna=False)

a      NaN

b    1.300

c      NaN

d   -0.275

dtype: float64

登录后复制

其他常用的统计方法有: 

######################## ******************************************
count 非 NA 值的数量
describe 针对 Series 或 DF 的列计算汇总统计
min , max 最小值和最大值
argmin , argmax 最小值和最大值的索引位置(整数)
idxmin , idxmax 最小值和最大值的索引值
quantile 样本分位数(0 到 1)
sum 求和
mean 均值
median 中位数
mad 根据均值计算平均绝对离差
var 方差
std 标准差
skew 样本值的偏度(三阶矩)
kurt 样本值的峰度(四阶矩)
cumsum 样本值的累计和
cummin , cummax 样本值的累计最大值和累计最小值
cumprod 样本值的累计积
diff 计算一阶差分(对时间序列很有用)
pct_change 计算百分数变化

 

处理缺失数据


pandas 中 NA 的主要表现为 np.nan,另外 Python 内建的 None 也会被当做 NA 处理。

处理 NA 的方法有四种:dropna , fillna , isnull , notnull 。 

is(not)null

这一对方法对对象做元素级应用,然后返回一个布尔型数组,一般可用于布尔型索引。 

dropna

对于一个 Series,dropna 返回一个仅含非空数据和索引值的 Series。

问题在于对 DataFrame 的处理方式,因为一旦 drop 的话,至少要丢掉一行(列)。这里的解决方式与前面类似,还是通过一个额外的参数:dropna(axis=0, how='any', thresh=None) ,how 参数可选的值为 any 或者 all。all 仅在切片元素全为 NA 时才抛弃该行(列)。另外一个有趣的参数是 thresh,该参数的类型为整数,它的作用是,比如 thresh=3,会在一行中至少有 3 个非 NA 值时将其保留。

fillna

fillna(value=None, method=None, axis=0) 中的 value 参数除了基本类型外,还可以使用字典,这样可以实现对不同的列填充不同的值。method 的用法与前面 .reindex() 方法相同,这里不再赘述。 

inplace 参数


前面有个点一直没讲,结果整篇示例写下来发现还挺重要的。就是 Series 和 DataFrame 对象的方法中,凡是会对数组作出修改并返回一个新数组的,往往都有一个 replace=False 的可选参数。如果手动设定为 True,那么原数组就可以被替换。

以上是pandas 基础的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板