Pandas武功修炼秘籍(第一章:初见端倪)


在学习pandas之前问自己三个问题

  • What is Pandas?
  • How to use Pandas?
  • 和其他数据处理第三方库相比,pandas有什么不同&相同之处?

第一个问题可以在pandas官方网站链接: link 找到答案

pandas is an open source BSD-licensed library providing high-performance, easy-to-use data structures and data analysis tools for the Python programming language.

从这一段话中我们可以知道如下信息:

  1. pandas是一个开源的第三方python库
  2. pandas主要用于数据分析,数据应用
  3. pandas提供了不止一种数据类型

第二个问题
How to use Pandas?

数据类型

要理解pandas的功能,就必须先了解pandas创建出对象的数据类型

Series

Series类型由一组数据及与之相关的数据索引组成,用于一维数组
简称:一组索引,一组值

DataFrame

DataFrame类型由共用相同索引的一组列组成,用于二维或多维数组
简称:一组索引,多组值

数据类型操作

创建对象
import pandas as pd
Series
#列表创建 
#注:顺序不可调换:值在前,索引在后. index可省略
pd.Series([1,2,3,4])
pd.Series([1,2,3,4],index=[0,1,2,3])
pd.Series([1,2,3,4],[0,1,2,3])
#range函数创建
pd.Series(range(1,5,1))
#ndarray创建
pd.Series(np.array(range(1,5,1)))
#字典创建
#字典中的key相当于Series中的index
pd.Series({0:1,1:2,2:3,3:4})
#index参数可以指定索引顺序
pd.Series({0:1,1:2,2:3,3:4},index=[0,1,2,3])
#以上运行结果皆为(除np是int32以外)
Out[94]: 
0    1
1    2
2    3
3    4
dtype: int64
DataFrame
#由Series&字典创建
pd.DataFrame({'one':pd.Series(range(1,3,1),['a','b']),'two':pd.Series([4,5],index=['a','b'])})
#由列表字典创建
pd.DataFrame({'one':[1,2],'two':[4,5]},index=['a','b'])
#上述两种创建方式结果均为:
Out[100]: 
   one  two
a    1    4
b    2    5
#从二维ndarray对象创建
pd.DataFrame(np.arange(10).reshape(2,5),index=['a','b'],columns=['one','two','three','four','five'])
Out[103]: 
   one  two  three  four  five
a    0    1      2     3     4
b    5    6      7     8     9
对象属性
#Series&DataFrame共有
n.values
n.index
#Series
n.name #需提前在创建时指定属性 n=np.Series(...,name='')
n.index.name
#DataFrame
n.columns
对象方法

适用于Series和DataFrame

get  #类似字典中的get方法
n.get(key, default=None)
key -- 字典中要查找的键。
default -- 如果指定键的值不存在时,返回该默认值。
in #判断值是否存在
存在返回True
不存在返回False
索引

pandas数据类型存在两种索引类型:

  • 数字索引( 从0开始 )

  • 自定义索引

DataFrame类型既有行索引也有列索引

#行索引
n.index
#列索引
n.columns
#值索引
n[c][i]

Series类型只有行索引

重新排序
n.reindex()

自定义行或列的排序

n.reindex(index=None, columns=None,)
index=[你希望重排后的顺序]
columns=[你希望重排后的顺序]
  • index, columns 新的行列自定义索引
  • fill_value 重新索引中,用于填充缺失位置的值
  • method 填充方法, ffill当前值向前填充,bfill向后填充
  • limit 最大填充量
  • copy 默认True,生成新的对象,False时,新旧相等不复制
n.sort_index()

对行或列进行升序或降序排序

n.sort_index(axis=0,ascending = False)
axis 轴 
#轴(axis): 保存数据的维度;秩(rank):轴的数量
axis=0 默认重排行
axis=1 重排列
ascending=False 升序(从小到大)
n.sort_values()

按值排序

DataFrame.sort_values(by, axis=0, ascending=True, inplace=False, kind='quicksort', na_position='last')  
#### 参数说明    
axis=0 默认重排行
axis=1 重排列   
by:str or list of str;
axis=0,by="列名"
axis=1,by="行名";  
ascending=True 升序(从小到大)
inplace:布尔型,是否用排序后的数据框替换现有的数据框  
kind:排序方法,{‘quicksort’, ‘mergesort’, ‘heapsort’}
默认是kind='quicksort'
na_position : {‘first’, ‘last’}
na_position='last'默认缺失值排在最后面  
增添&修改
增添行/列

insert方法 索引类型方法 (作用对象为index类型)

  1. 创建一个索引类型,调用insert方法
#在loc位置增加一个元素e
d.index.insert(loc,e) 
d.columns.insert(loc,e)
  1. 利用reindex方法替换掉旧的行/列
n = pd.DataFrame(np.arange(10).reshape(2,5),index=['a','b'],columns=['one','two','three','four','five'])
Out[103]: 
   one  two  three  four  five
a    0    1      2     3     4
b    5    6      7     8     9
ni=n.index.insert(2,'c')
nc=n.columns.insert(5,'six')
n.reindex(index=ni,columns=nc,fill_value=0)
Out[154]: 
   one  two  three  four  five  six
a    0    1      2     3     4    0
b    5    6      7     8     9    0
c    0    0      0     0     0    0
删除
  • delete方法 索引类型方法
.delete(loc) 删除loc位置处的元素
用法与insert相同
  • drop
    删除Series和DataFrame指定行或列索引
n.drop(index,axis=0)
axis=0 默认是行
axis=1

数据运算

数据运算时,倾向将对象看做一个整体,类似行列式,矩阵;对整体进行运算

同维度
  • 算数运算
    自动补齐,索引相对应相加减
  • 比较运算
    产生布尔对象,不补齐
不同维度

默认在1轴(即航运算),如果想要进行列运算,要制定axis=0


第三个问题
和其他数据处理第三方库相比,pandas有什么不同&相同之处?
numpy基础数据类型,常与pandas搭配使用
pandas扩展的数据类型
numpy更注重数据维度
pandas更注重数据与索引之间的关系和运算

一种观念只能生长在一堆观念之上,学习新事物尤是如此

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值