美文网首页
Python学习笔记(8):Pandas库入门1

Python学习笔记(8):Pandas库入门1

作者: TOP生物信息 | 来源:发表于2019-03-19 16:00 被阅读0次
1. 安装

在Windows终端命令行下,pip install pandas

2. Series类型

Series类型由一组数据及与之相关的数据索引组成(一维带“标签”数组)

2.1 创建

从标量值创建

a=pd.Series(1,index=['a', 'b', 'c']) # 索引不能省
a
a    1
b    1
c    1

从字典类型创建

b=pd.Series({'a':1, 'b':2, 'c':3})
b
a    1
b    2
c    3

b=pd.Series({'a':1, 'b':2, 'c':3}, index=['a', 'c', 'b']) # 根据索引自定义顺序
b
a    1
c    3
b    2

从ndarray类型创建

c=pd.Series(np.arange(3))
c
0    0
1    1
2    2

c=pd.Series(np.arange(3), index=np.arange(3, 0, -1))
c
3    0
2    1
1    2

总结
Series类型可以由如下类型创建:
• Python列表,index与列表元素个数一致
• 标量值,index表达Series类型的尺寸
• Python字典,键值对中的“键”是索引,index从字典中进行选择操作
• ndarray,索引和数据都可以通过ndarray类型创建
• 其他函数,range()函数等

2.2 基本操作

总结
Series类型的操作类似ndarray类型:
• 索引方法相同,采用[]
• NumPy中运算和操作可用于Series类型
• 可以通过自定义索引的列表进行切片
• 可以通过自动索引进行切片,如果存在自定义索引,则一同被切片

d=pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd'])

d.index # 获取索引
Index(['a', 'b', 'c', 'd'], dtype='object')
d.values # 获取值
array([1, 2, 3, 4], dtype=int64)

>>> d[0] # 自动索引和自定义索引并存,但不能混用
1
>>> d['a']
1

总结
Series类型的操作类似Python字典类型:
• 通过自定义索引访问
• 保留字in操作
• 使用.get()方法

>>> 'c' in d.index
True
>>> 3 in d.values
True
>>> d.get('d')
4
3. DataFrame类型

DataFrame类型由共用相同索引的一组列组成

3.1 创建

总结
• 二维ndarray对象
• 由一维ndarray、列表、字典、元组或Series构成的字典
• Series类型
• 其他的DataFrame类型
从二维ndarray对象创建

>>> e=pd.DataFrame(np.arange(20).reshape(4, 5))
>>> e
    0   1   2   3   4
0   0   1   2   3   4
1   5   6   7   8   9
2  10  11  12  13  14
3  15  16  17  18  19

从一维ndarray对象字典创建

>>> dt = { 'one': pd.Series(np.arange(5), index=['a', 'b', 'c', 'd', 'e']), 
'two': pd.Series(np.arange(5, 9), index=['a', 'b', 'c', 'd'])}

>>> f=pd.DataFrame(dt)
>>> f
   one  two
a    0  5.0
b    1  6.0
c    2  7.0
d    3  8.0
e    4  NaN

>>> f=pd.DataFrame(dt, index=['c', 'd', 'e', 'f'], columns=['one', 'two', 'three'])
>>> f
   one  two three
c  2.0  7.0   NaN
d  3.0  8.0   NaN
e  4.0  NaN   NaN
f  NaN  NaN   NaN
# 数据根据行列索引自动补齐

从列表类型的字典创建

>>> dt={'one': [1, 2, 3, 4], 'two': [5, 6, 7, 8]}
>>> g=pd.DataFrame(dt)
>>> g
   one  two
0    1    5
1    2    6
2    3    7
3    4    8
3.2 基本操作
>>> g.columns
Index(['one', 'two'], dtype='object')
>>> g.index
RangeIndex(start=0, stop=4, step=1)
>>> g.values
array([[1, 5],
       [2, 6],
       [3, 7],
       [4, 8]], dtype=int64)

>>> g['one']
0    1
1    2
2    3
3    4
Name: one, dtype: int64
>>> g.ix[0]
one    1
two    5
Name: 0, dtype: int64
>>> g['one'][0] # 先列名后行名
1
4. 数据类型操作
4.1 重新索引

.reindex()能够改变或重排Series和DataFrame索引

>>> g=g.reindex(index=[3, 2, 1, 0])
>>> g
   one  two
3    4    8
2    3    7
1    2    6
0    1    5
>>> g.reindex(columns=['two', 'one'])
   two  one
3    8    4
2    7    3
1    6    2
0    5    1
.reindex参数 说明
index, columns 新的行列自定义索引
fill_value 重新索引中,用于填充缺失位置的值
method 填充方法, ffill当前值向前填充,bfill向后填充
limit 最大填充量
copy 默认True,生成新的对象,False时,新旧相等不复制
>>> g
   one  two
0    1    5
1    2    6
2    3    7
3    4    8
>>> new_column=g.columns.insert(2, 'three')
>>> g.reindex(columns=new_column, fill_value=9)
   one  two  three
0    1    5      9
1    2    6      9
2    3    7      9
3    4    8      9
4.2 索引类型

常用方法

方法 说明
.append(idx) 连接另一个Index对象,产生新的Index对象
.diff(idx) 计算差集,产生新的Index对象
.intersection(idx) 计算交集
.union(idx) 计算并集
.delete(loc) 删除loc位置处的元素
.insert(loc,e) 在loc位置增加一个元素e
4.3 .drop()删除指定行或列索引
>>> g.drop(0, axis=0)
   one  two
1    2    6
2    3    7
3    4    8
5. 数据类型运算
5.1 算术运算
  • 算术运算根据行列索引,补齐后运算(补齐时缺项填充NaN),运算默认产生浮点数
  • 二维和一维、一维和零维间为广播运算
  • 采用+ ‐ * /符号进行的二元运算产生新的对象
>>> a=pd.DataFrame(np.arange(9).reshape(3, 3))
>>> b=pd.DataFrame(np.arange(12).reshape(4, 3))
>>> a
   0  1  2
0  0  1  2
1  3  4  5
2  6  7  8
>>> b
   0   1   2
0  0   1   2
1  3   4   5
2  6   7   8
3  9  10  11
>>> a+b
      0     1     2
0   0.0   2.0   4.0
1   6.0   8.0  10.0
2  12.0  14.0  16.0
3   NaN   NaN   NaN
>>> a*b
      0     1     2
0   0.0   1.0   4.0
1   9.0  16.0  25.0
2  36.0  49.0  64.0
3   NaN   NaN   NaN

常用方法

方法 说明
.add(d, **argws) 类型间加法运算,可选参数
.sub(d, **argws) 类型间减法运算,可选参数
.mul(d, **argws) 类型间乘法运算,可选参数
.div(d, **argws) 类型间除法运算,可选参数
>>> a.add(b, fill_value=0) # fill_value代替NaN
      0     1     2
0   0.0   2.0   4.0
1   6.0   8.0  10.0
2  12.0  14.0  16.0
3   9.0  10.0  11.0
5.2 比较运算
  • 比较运算只能比较相同索引的元素,不进行补齐
  • 二维和一维、一维和零维间为广播运算
  • 采用> < >= <= == !=等符号进行的二元运算产生布尔对象
>>> a
   0  1  2
0  0  1  2
1  3  4  5
2  6  7  8
>>> b
   0  1  2
0  1  2  3
1  4  5  6
2  7  8  9
>>> a > b
       0      1      2
0  False  False  False
1  False  False  False
2  False  False  False
>>> a > 4
       0      1      2
0  False  False  False
1  False  False   True
2   True   True   True

相关文章

网友评论

      本文标题:Python学习笔记(8):Pandas库入门1

      本文链接:https://www.haomeiwen.com/subject/swwimqtx.html