pandas(零)数据结构详解大数据
series是一种类似于一维数组的对象,它由一组数据(NumPy数组类型的数据)和一组与之相关的数据标签(索引)组成。
1 from pandas import Series 3 obj = Series((1,2,3,4,5)) 4 obj.index #索引,默认从0开始的整数 5 obj.values #array数组 7 #自定义索引的Series 8 obj_with_index = Series([4,65,3,4],index=[a,b,c,d]) 9 print(obj_with_index)
可以通过索引的方式选取或修改单个或一组值
1 from pandas import Series 2 obj = Series([1,2,3,4],index=(a,b,c,d)) 3 obj 4 a 1 5 b 2 6 c 3 7 d 4 8 dtype: int64 9 obj[a] 10 1 11 obj[c] = 8 12 obj 13 a 1 14 b 2 15 c 8 16 d 4 18 obj[(a,b,d)] = (11,12,14) 19 obj 20 a 11 21 b 12 22 c 8 23 d 14 24 dtype: int64 25
对其对象进行NumPy运算的时候会保留索引和值之间的链接
obj -2 b 10 d 12 dtype: int64 obj[obj 10] a 11 b 12 d 14 dtype: int64
还可以将Series对象看成一个定长的有序的字典,一个索引和值组成的键值对的映射。
可以应用在很多需要字典参数的函数中,例如函数的关键字参数
利用字典作为参数生成一个自定义索引的Series对象
metadata = {"a":1,"b":2,"c":3} data= Series(metadata) data dtype: int64
根据索引去建对象,如果字典的键中没有在索引中,则不会创建该索引-值的映射,索引不在字典的键中,则会对该索引创建一个Nan值(表示缺失或NA值)
可以利用isnull和notnull去判断是不是Nan值
states = [b,c,d,e] obj4 = Series(metadata,index = states) obj4 b 2.0 c 3.0 d NaN e NaN dtype: float64 import pandas as pd pd.isnull(obj4) b False c False d True e True dtype: bool pd.notnull(obj4) b True c True d False e False dtype: bool #series对象本身就有isnull和notnull方法 obj4.isnull() b False c False d True e True dtype: bool obj4.notnull() b True c True d False e False dtype: bool
Series一个最重要的功能就是在算术运算中会自动对齐索引。
Series对象及其索引都有一个name属性
dtype: bool obj4.name ="numpy_array" obj4.index.name = letter obj4 letter b 2.0 c 3.0 d NaN e NaN Name: numpy_array, dtype: float64DataFrame:
DataFrame是一个表格型的数据结构,它含有一组有序的列,每一列可以是不同的值类型。DataFrame不仅有列索引,还有行索引。DataFrame中的数据是以一个或多个二维块存放的。
用等长列表或NumPy数组组成的字典去创建DataFrame对象
data = {name:[li,wang,yang,sun],height:[1.7,1.8,1.6,1.55],age:[30,31,34,61]} df_obj = DataFrame(data) df_obj age height name 0 30 1.70 li 1 31 1.80 wang 2 34 1.60 yang 3 61 1.55 sun
如果原始数据是嵌套的字典,会将外层字典的建作为列,内层字典的键做为行索引
如果指定了列序列,就会按照指定的列序列的顺序进行排列,行序列会自动生成
df_obj = DataFrame(data,columns=[name,age,height]) df_obj name age height 0 li 30 1.70 1 wang 31 1.80 2 yang 34 1.60 3 sun 61 1.55
如果传入的列在数据中找不到也会产生Nan值
df_obj = DataFrame(data,columns=[name,age,height,weight],index=(a,b,c,d)) df_obj name age height weight a li 30 1.70 NaN b wang 31 1.80 NaN c yang 34 1.60 NaN d sun 61 1.55 NaN
可以通过字典标记的方式或属性的方式,将DataFrame的列获取为一个Series:
df_obj.columns Index([name, age, height, weight], dtype=object) df_obj.name a li b wang c yang d sun Name: name, dtype: object df_obj[name] a li b wang c yang d sun Name: name, dtype: object
ps:在获取列的时候不仅继承了原来的索引,还将Series的name属性设置好了
也可以给列赋值,但如何是将列表或数组赋值给某列,其长度必须跟DataFrame的长度相同,如果复制的是一个Series对象,就会根据索引去精确匹配
df_obj.weight = 200 df_obj name age height weight a li 30 1.70 200 b wang 31 1.80 200 c yang 34 1.60 200 d sun 61 1.55 200
删除列用del关键字
del df_obj[weight]#注意:这里不能用列的属性,即:del df_obj.weigt是错误的 df_obj name age height a li 30 1.70 b wang 31 1.80 c yang 34 1.60 d sun 61 1.55
dataframe对象也可以转置
df_obj.T a b c d name li wang yang sun age 30 31 34 61 height 1.7 1.8 1.6 1.55索引对象
index对象不能被修改,这样保证了多个数据结构能够安全共享
创建索引对象及在Series或DataFrame中使用
index = pd.Int64Index(range(1,6)) index Int64Index([1, 2, 3, 4, 5], dtype=int64) data = Series((11,22,33,44,55),index=index) data 1 11 2 22 3 33 4 44 5 55 index.append(pd.Index((6,))) Int64Index([1, 2, 3, 4, 5, 6], dtype=int64)
pandas的索引对象
相关文章
- panda’_pandas map
- pandas中的loc和iloc_pandas获取指定数据的行和列
- pycharm下载pandas包失败_pycharm下载包很慢
- 10个快速入门Query函数使用的Pandas的查询示例
- Python使用pandas读取excel表格数据
- Pandas光速入门-一文掌握数据操作
- 25个例子学会Pandas Groupby 操作(附代码)
- Python pandas按列拆分Excel为多个文件
- Pandas 拆分总表为多文件,一个文件有多个工作表
- pandas_VS_Excel提取各班前2名后2名的数据
- pandas合并多个小Excel到一个大 Excel
- pandas.DataFrame()中的iloc和loc用法
- 数据导入与预处理-拓展-pandas筛选与修改
- 【硬核干货】Pandas模块中的数据类型转换
- 整理了10个经典的Pandas数据查询案例
- 没用Pandas快捷方法,硬核编程的我面试被拒
- Pandas | 数据统计
- Pandas处理大数据的性能优化技巧
- 软件测试|数据处理神器pandas教程(六)
- pandas(一)操作Series和DataFrame的基本功能详解大数据
- 使用 Pandas 在 Python 中绘制数据