pandas知识点(数据结构)
In [3]: obj = Series([1,2,3,4,5])
In [4]: obj
Out[4]:
0 1
1 2
2 3
3 4
4 5
dtype: int64
In [5]: obj.values
Out[5]: array([1, 2, 3, 4, 5], dtype=int64)
In [6]: obj.index
Out[6]: RangeIndex(start=0, stop=5, step=1)
创建对各个数据点进行标记的索引:
In [7]: obj2 = Series([4,1,9,7], index=["a","c","e","ff"])
In [8]: obj2
Out[8]:
a 4
c 1
e 9
ff 7
dtype: int64
In [9]: obj2.index
Out[9]: Index(['a', 'c', 'e', 'ff'], dtype='object')
取一个值或一组值:
In [10]: obj2["c"]
Out[10]: 1
In [11]: obj2[["c","e"]]
Out[11]:
c 1
e 9
dtype: int64
数组运算,会显示索引:
In []: obj2[obj2>]
Out[]:
a
e
ff
dtype: int64
In [13]: "c" in obj2
Out[13]: True
In [14]: data = {"name":"liu","year":18,"sex":"man"}
In [15]: obj3 = Series(data)
In [16]: obj3
Out[16]:
name liu
year 18
sex man
dtype: object
In [17]: list1 = ["name","year","mobile"]
In [18]: obj4 = Series(data,index=list1)
In [19]: obj4
Out[19]:
name liu
year 18
mobile NaN
dtype: object
PS:因为data字典中没有mobile所以值为NaN
In [20]: pd.isnull(obj4)
Out[20]:
name False
year False
mobile True
dtype: bool In [21]: pd.notnull(obj4)
Out[21]:
name True
year True
mobile False
dtype: bool In [22]: obj4.isnull()
Out[22]:
name False
year False
mobile True
dtype: bool In [23]: obj4.notnull()
Out[23]:
name True
year True
mobile False
dtype: bool
In [7]: obj4.name = "hahaha"
In [8]: obj4.index.name = "state"
In [9]: obj4
Out[9]:
state
name liu
year 18
mobile NaN
Name: hahaha, dtype: object
In [13]: data = {
"state":[1,1,2,1,1],
"year":[2000,2001,2002,2004,2005],
"pop":[1.5,1.7,3.6,2.4,2.9]
}
In [14]: frame = DataFrame(data)
In [15]: frame
Out[15]:
state year pop
0 1 2000 1.5
1 1 2001 1.7
2 2 2002 3.6
3 1 2004 2.4
4 1 2005 2.9
In [18]: frame2 = DataFrame(
data,
columns=["year","state","pop","debt"],
index=["one","two","three","four","five"]
)
In [19]: frame2
Out[19]:
year state pop debt
one 2000 1 1.5 NaN
two 2001 1 1.7 NaN
three 2002 2 3.6 NaN
four 2004 1 2.4 NaN
five 2005 1 2.9 NaN
In [7]: frame2.year
Out[7]:
one 2000
two 2001
three 2002
four 2004
five 2005
Name: year, dtype: int64
PS:返回的索引不变,且name属性被设置了
In [11]: frame2.loc["three"]
Out[11]:
year 2002
state 2
pop 3.6
debt NaN
Name: three, dtype: object
In [12]: frame2['debt'] = 16.5
In [13]: frame2
Out[13]:
year state pop debt
one 2000 1 1.5 16.5
two 2001 1 1.7 16.5
three 2002 2 3.6 16.5
four 2004 1 2.4 16.5
five 2005 1 2.9 16.5
In [17]: val = Series([1.2,1.5,1.7], index=["two","four","five"])
In [18]: frame2['debt'] = val
In [19]: frame2
Out[19]:
year state pop debt
one 2000 1 1.5 NaN
two 2001 1 1.7 1.2
three 2002 2 3.6 NaN
four 2004 1 2.4 1.5
five 2005 1 2.9 1.7
In [21]: frame2["eastern"] = frame2.state == 1
In [22]: frame2
Out[22]:
year state pop debt eastern
one 2000 1 1.5 NaN True
two 2001 1 1.7 1.2 True
three 2002 2 3.6 NaN False
four 2004 1 2.4 1.5 True
five 2005 1 2.9 1.7 True
In [23]: dic = {"name":{"one":"liu","two":"rui"},"year":{"one":"","two":""}}
In [24]: frame3 = DataFrame(dic)
In [25]: frame3
Out[25]:
name year
one liu 23
two rui 22
In [26]: frame3.index.name = "index"
In [27]: frame3.columns.name = "state"
In [28]: frame3
Out[28]:
state name year
index
one liu 23
two rui 22
In [29]: frame3.values
Out[29]:
array([['liu', ''],
['rui', '']], dtype=object)
In [30]: obj = Series(range(3),index=["a","b","c"])
In [31]: index = obj.index
In [32]: index
Out[32]: Index(['a', 'b', 'c'], dtype='object')
In [35]: index = pd.Index(np.arange(3))
In [36]: obj2 = Series([1.5,0.5,2],index=index)
In [37]: obj2.index is index
Out[37]: True
pandas知识点(数据结构)的更多相关文章
- 机器学习-Pandas 知识点汇总(吐血整理)
Pandas是一款适用很广的数据处理的组件,如果将来从事机械学习或者数据分析方面的工作,咱们估计70%的时间都是在跟这个框架打交道.那大家可能就有疑问了,心想这个破玩意儿值得花70%的时间吗?咱不是还 ...
- 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的创建 ...
- Python数据分析--Pandas知识点(三)
本文主要是总结学习pandas过程中用到的函数和方法, 在此记录, 防止遗忘. Python数据分析--Pandas知识点(一) Python数据分析--Pandas知识点(二) 下面将是在知识点一, ...
- Pandas的使用(3)---Pandas的数据结构
Pandas的使用(3) Pandas的数据结构 1.Series 2.DataFrame
- Pandas之数据结构
pandas入门 由于最近公司要求做数据分析,pandas每天必用,只能先跳过numpy的学习,先学习大Pandas库 Pandas是基于Numpy构建的,让以Numpy为中心的应用变得更加简单 pa ...
- Python数据分析--Pandas知识点(二)
本文主要是总结学习pandas过程中用到的函数和方法, 在此记录, 防止遗忘. Python数据分析--Pandas知识点(一) 下面将是在知识点一的基础上继续总结. 13. 简单计算 新建一个数据表 ...
- pandas知识点脑图汇总
参考文献: [1]Pandas知识点脑图汇总
- pandas中数据结构-Series
pandas中数据结构-Series pandas简介 Pandas是一个开源的,BSD许可的Python库,为Python编程语言提供了高性能,易于使用的数据结构和数据分析工具.Python与Pan ...
随机推荐
- 牛客网Java刷题知识点之表达式类型的自动提升
不多说,直接上干货!
- Spark Mllib里相似度度量(基于余弦相似度计算不同用户之间相似性)(图文详解)
不多说,直接上干货! 常见的推荐算法 1.基于关系规则的推荐 2.基于内容的推荐 3.人口统计式的推荐 4.协调过滤式的推荐 协调过滤算法,是一种基于群体用户或者物品的典型推荐算法,也是目前常用的推荐 ...
- idea没有绑远程地址,如何提交到github的空项目
一 有同事问我怎么提交到github的空项目....这么简单.... 二 1.创建本地的git仓库 选择自己的项目 创建成功 2.添加代码,选中左边的文件,add 3.提交 5.定义远程仓库,不定义的 ...
- asp.net MVC 中枚举创建下拉列表?
我将尝试使用 Html.DropDownList 扩展方法,但不能找出如何使用它的枚举. 让我们说我有一个这样的枚举: public enum ItemTypes { Movie = 1, Game ...
- maven(多个模块)项目 部署 开发环境 问题处理历程【异常Name jdbc is not bound in this Context 异常java.lang.NoSuchMethodE】
maven(多个模块)项目 部署 开发环境 问题处理历程[异常Name jdbc is not bound in this Context 异常java.lang.NoSuchMethodE] 201 ...
- 用C++/CLI搭建C++和C#之间的桥梁
一.简单用法 C#和C++是非常相似的两种语言,然而我们却常常将其用于两种不同的地方,C#得益于其简洁的语法和丰富的类库,常用来构建业务系统.C++则具有底层API的访问能力和拔尖的执行效率,往往用于 ...
- sublimetext3安装配置
官网:http://www.sublimetext.com/ 中文官网:http://www.sublimetextcn.com/ 包管理器:https://packagecontrol.io/ins ...
- Java栈,PC寄存器,本地方法栈,堆,方法区(静态区)和运行常量池
详情参考:https://my.oschina.net/wangsifangyuan/blog/711329 前言:当要判断一个变量存在什么空间上哪儿时,先分析它是哪一种(是实例变量还是局部变量),实 ...
- git记录
2017-3-30:git常用命令:1.$ git init:初始化git仓库2.$ git add *.c:跟踪文件3.$ git commit -m 'initial project versio ...
- HDU 1011 Starship Troopers星河战队(树形dp)
题意 有n个洞穴编号为1-n,洞穴间有通道,形成了一个n-1条边的树, 洞穴的入口即根节点是1. 每个洞穴有x只bugs,并有价值y的金子,全部消灭完一个洞穴的虫子,就可以获得这个洞穴的y个金子. 现 ...