pandas(零)数据结构
pandas的两个主要的数据结构:
Series
series是一种类似于一维数组的对象,它由一组数据(NumPy数组类型的数据)和一组与之相关的数据标签(索引)组成。
from pandas import Series obj = Series((1,2,3,4,5))
obj.index #索引,默认从0开始的整数
obj.values #array数组 #自定义索引的Series
obj_with_index = Series([4,65,3,4],index=['a','b','c','d'])
print(obj_with_index)
可以通过索引的方式选取或修改单个或一组值
>>> from pandas import Series
>>> obj = Series([1,2,3,4],index=('a','b','c','d'))
>>> obj
a 1
b 2
c 3
d 4
dtype: int64
>>> obj['a']
1
>>> obj['c'] = 8
>>> obj
a 1
b 2
c 8
d 4 >>> obj[('a','b','d')] = (11,12,14)
>>> obj
a 11
b 12
c 8
d 14
dtype: int64
>>>
对其对象进行NumPy运算的时候会保留索引和值之间的链接
>>> obj -2
a 9
b 10
c 6
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
a 1
b 2
c 3
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: float64
DataFrame:
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的索引对象
类 | 说明 |
Index | 最泛化的Index对象,将轴标签表示为一个由python对象组成的NumPy数组 |
Int64Index | 针对整数的特殊Index |
MultiIndex | 层次化索引对象,表示单个轴上的多层索引。可以看作由元组组成的数组 |
DatatimeIndex | 存储纳秒级时间戳 |
PeriodIndex | 针对Period数据(时间间隔)的特殊Index |
Index对象的方法与属性
方法 | 属性 |
---|---|
append | 链接另一个index对象,产生一个新的Index |
diff | 计算差集,并得到一个Index |
intersection | 计算交集 |
union | 计算并集 |
isin | 计算一个指示各值是否都包含在参数集合中的布尔型数组 |
delete | 产出索引i出的元素,并得到新的Index |
drop | 删除传入的值,并得到新的Index |
insert | 将元素插入到索引i处,并得到新的Index |
is_monotonic | 将各元素均大于等于前一个元素时,返回True |
is_unique | 将Index没有重复值时,返回True |
unique | 返回Index中唯一的数组 |
pandas(零)数据结构的更多相关文章
- Pandas 的数据结构
Pandas的数据结构 导入pandas: 三剑客 from pandas import Series,DataFrame import pandas as pd import numpy as np ...
- pandas的数据结构之series
Pandas的数据结构 1.Series Series是一种类似于一维数组的对象,由下面两个部分组成: index:相关的数据索引标签 values:一组数据(ndarray类型) series的创建 ...
- Pandas的使用(3)---Pandas的数据结构
Pandas的使用(3) Pandas的数据结构 1.Series 2.DataFrame
- Pandas之数据结构
pandas入门 由于最近公司要求做数据分析,pandas每天必用,只能先跳过numpy的学习,先学习大Pandas库 Pandas是基于Numpy构建的,让以Numpy为中心的应用变得更加简单 pa ...
- pandas中数据结构-Series
pandas中数据结构-Series pandas简介 Pandas是一个开源的,BSD许可的Python库,为Python编程语言提供了高性能,易于使用的数据结构和数据分析工具.Python与Pan ...
- 02. Pandas 1|数据结构Series、Dataframe
1."一维数组"Series Pandas数据结构Series:基本概念及创建 s.index . s.values # Series 数据结构 # Series 是带有标签的一 ...
- Python数据分析Pandas库数据结构(一)
pandas数据结构 1.生成一维矩阵模拟数据 import pandas as pdimport numpy as nps = pd.Series([1,2,3,4,np.nan,9,9])s2 = ...
- pandas 的数据结构(Series, DataFrame)
Pandas 讲解 Python Data Analysis Library 或 pandas 是基于NumPy 的一种工具,该工具是为了解决数据分析任务而创建的. Pandas 纳入了大量库和一些标 ...
- pandas 的数据结构Series与DataFrame
pandas中有两个主要的数据结构:Series和DataFrame. [Series] Series是一个一维的类似的数组对象,它包含一个数组数据(任何numpy数据类型)和一个与数组关联的索引. ...
- pandas的数据结构
要使用pandas,需要熟悉它的两个主要的数据结构,Series和DataFrame. Series series是一种类似于以为数组的对象,它由一组数据(各种numpy的数据类型)以及一组与之相关的 ...
随机推荐
- leetCode 30.Substring with Concatenation of All Words (words中全部子串相连) 解题思路和方法
Substring with Concatenation of All Words You are given a string, s, and a list of words, words, tha ...
- 基于RocketIO的高速串行协议设计与实现
随着对信息流量需求的不断增长, 传统并行接口技术成为进一步提高数据传输速率的瓶颈.过去主要用于光纤通信的串行通信技术—SERDES正在取代传统并行总线而成为高速接口技术的主流.SERDES 是串行器) ...
- typeof()关键字
typeof是GNU c标准的关键字. typeof()的作用是自动推导出括号中表达式的数据类型. #include <stdio.h> void func1(void) { ; type ...
- 使用sklean进行多分类下的二分类
#coding:utf-8 import matplotlib.pyplot as plt import numpy as np from sklearn.linear_model import Lo ...
- There's no Qt version assigned to this project for platform Win32. Please use the 'change Qt version' feature and choose a valid Qt version for this platform.
这个是用在vs2015时爆出的问题. 解决方法是: 1.鼠标放置在解决方案中的工程名处,用鼠标右键点击(右击). 2.选择Qt Project Settings,在弹出的对话框中的version栏处填 ...
- EEPlat的元模型体系
EEPlat的元模型体系是元数据驱动的必要条件之中的一个.仅仅有通过元模型可以完好的描写叙述一个软件系统.才可以完整的定义该软件系统的元数据,也才干真正实现软件系统的元数据驱动式开发.也就意味着一个软 ...
- Proving NP-completeness
Proving NP-completeness by generalization. For each of the problems below, prove that it is NP-compl ...
- Play on Words UVA - 10129 欧拉路径
关于欧拉回路和欧拉路径 定义:欧拉回路:每条边恰好只走一次,并能回到出发点的路径欧拉路径:经过每一条边一次,但是不要求回到起始点 ①首先看欧拉回路存在性的判定: 一.无向图每个顶点的度数都是偶数,则存 ...
- GCD - Extreme (II) for(i=1;i<N;i++) for(j=i+1;j<=N;j++) { G+=gcd(i,j); } 推导分析+欧拉函数
/** 题目:GCD - Extreme (II) 链接:https://vjudge.net/contest/154246#problem/O 题意: for(i=1;i<N;i++) for ...
- Marple表演电影字幕
119501:15:59,702 --> 01:16:02,782我的幸运死了 而我很清楚是谁杀了她的 (格雷格)My Lucky is dead, and I know perfectly w ...