【Python数据分析与展示】(四)pandas库基本操作
来源:互联网 发布:家装erp软件 编辑:程序博客网 时间:2024/06/05 21:16
Series
Series是由一组数据和数据的索引构成
import numpy as npimport pandas as pda = pd.Series ([9,8,7,6],index = ['a','b','c','d']) #如果index处于属性的第二位,可以省略“index =”#a 9 b 8 c 7 d 6dtype: int64a = pd.Series (25,index = ['a','b','c','d']) #不能省略index = d= {'a':1,'b':5,'c':7}b = pd.Series(d) #从字典创建Seriesb = pd.Series(d,index = ["b","c","d"])#b 5.0 c 7.0 d NaNdtype: float64e = pd.Series (np.arange(0,5),index = np.arange(9,4,-1)) #从ndarray创建Series# 9 0 8 1 7 2 6 3 5 4dtype: int32b.values #array([ 5., 7., nan])b.index #Index(['b', 'c', 'd'], dtype='object')b[1] #7.0 index的默认索引,如果自定义索引是数字,则不能使用默认索引了b['b'] #5.0f = pd.Series([9,8,7,6],index = ['a','b','c','d'])#a 9 b 8 c 7 d 6dtype: int64np.exp(f) #nparray的操作可以用于Series#a 8103.083928 b 2980.957987 c 1096.633158 d 403.428793dtype: float64f[:3]#a 9b 8c 7dtype: int64f[f>7]#a 9b 8dtype: int64g =pd.Series([1,2,3,5],index = ['b','c','d',"e"])f+g #注意结果中索引的对齐#a NaNb 9.0c 9.0d 9.0e NaNdtype: float64f.name = "f Series 对象"f.index.name = "索引咧名字"#索引咧名字a 9b 8c 7d 6Name: f Series 对象, dtype: int64f['a'] = 18 #直接修改索引咧名字a 18b 8c 7d 6Name: f Series 对象, dtype: int64
DataFrame
由共用相同的索引的一组列构成,或者可以说是带行列索引的二维数组
a = pd.DataFrame(np.arange(0,10).reshape(5,2)) # 由二维数组ndarray创建a# 0 10 0 11 2 32 4 53 6 74 8 9dt = {"one":pd.Series([1,2,3],index = ['a','b','c']), "two":pd.Series([9,8,7,6],index = ['a','b','c','d'])}d = pd.DataFrame(dt)# one twoa 1.0 9b 2.0 8c 3.0 7d NaN 6d.columns= ["一","二"] #修改列名d.columns d1 = {"one":[1,2,3,4],"two":[5,6,7,8]} #从列表类型的字典创建pd.DataFrame(d1,index = ['a','b','c','d'])#one twoa 1 5b 2 6c 3 7d 4 8d.reindex(index =['a','d','c','b'],columns = ["二","一"],fill)@ 二 一a 9 1.0d 6 NaNc 7 3.0b 8 2.0
.index .columns 的索引是index类型,不可修改。.drop能删除指定的Series和DataFrame的行或列,删除列的时候需要给出参数,axis =1
索引的常用操作
pandas运算·
b1 = pd.DataFrame(np.arange(0,20).reshape(4,5)) # 0 1 2 3 40 0 1 2 3 41 5 6 7 8 92 10 11 12 13 143 15 16 17 18 19c = pd.Series(np.arange(4))#0 01 12 23 3dtype: int32b - c #默认发生在1轴上,也就是按列计算#0 1 2 3 40 0.0 0.0 0.0 0.0 NaN1 5.0 5.0 5.0 5.0 NaN2 10.0 10.0 10.0 10.0 NaN3 15.0 15.0 15.0 15.0 NaNb1.sub(c,axis = 0) # 0 1 2 3 40 0 1 2 3 41 4 5 6 7 82 8 9 10 11 123 12 13 14 15 16#算数运算 .add() .sub() .mul() .div()#比较运算 同纬度运算,尺寸要一致,不同纬度广播,默认发生在1轴
阅读全文
0 0
- 【Python数据分析与展示】(四)pandas库基本操作
- Python数据分析库pandas基本操作
- 【Python数据分析与展示】(五)pandas库数据分析
- Python数据分析与展示(5)——Pandas库入门
- 用python做数据分析|pandas库:DataFrame基本操作
- Python数据分析与展示(6)——Pandas数据特征分析
- python数据分析:pandas数据结构与操作
- python数据分析与展示(四)
- #######用python做数据分析4|pandas库介绍之DataFrame基本操作#######
- 用python做数据分析4|pandas库介绍之DataFrame基本操作
- 用python做数据分析4|pandas库介绍之DataFrame基本操作
- 用python做数据分析4|pandas库介绍之DataFrame基本操作
- 用python做数据分析4|pandas库介绍之DataFrame基本操作
- 用python做数据分析4|pandas库介绍之DataFrame基本操作
- 利用Python数据分析:pandas入门(四)
- python数据挖掘包Pandas基本操作
- 利用Pandas进行数据分析(2)——pandas数据基本操作
- 【Python数据分析与展示】(三)matplotlib库pyplot
- 瞎搞-系列 Linux 如何查看进程,文件传输,更换源,进行xface桌面
- 模型的过分拟合
- 大数据量Elastic数据迁移方法及注意事项
- #求助#搞到现在凌晨3点,卡到这上面了【error C2248: “CObject::CObject”: 无法访问 private 成员(在“CObject”类中声明)】
- logstash与filebeat收集日志
- 【Python数据分析与展示】(四)pandas库基本操作
- JAVA高并发学习(一)
- 你或许理解错了Android系统权限管理的这两个概念
- STC15单片机驱动WS2812
- 12.12 IE7275-Data Mining笔记
- 多进程、进程池
- 如何成功配置opencv3 sfm模块?
- ios-消息转发机制
- spring boot整合freemarker出现Whitelabel Error Page问题