数据结构5 :DataFrame(Pandas库)
Pandas 的数据结构主要是:Series(一维数组),DataFrame(二维数组)。DataFrame是由索引和内容组成,索引既有行索引index又有列索引columns,如 内容,index=[],colunms=[] 这样的形式。
1 Pandas中创建DataFrame
1.1 pd.DataFrame(ndarray数据,index=[‘行索引1’,‘行索引2’],colunms=[‘列索引1’,‘列索引2’])
import numpy as np
import pandas as pd
a=pd.DataFrame(np.arange(18).reshape(3,6),index=['a','b','c'],columns=['A','B','C','D','E','F'])
print(a)
# A B C D E F
# a 0 1 2 3 4 5
# b 6 7 8 9 10 11
# c 12 13 14 15 16 17
1.2 pd.DataFrame(dict数据)
a=pd.DataFrame([{'a':0,'b':3,'c':6},{'a':1,'b':4,'c':7},{'a':2,'b':8,'c':5}]) #带字典的列表
b=pd.DataFrame({'a':[0,1,2],'b':[3,4,8],'c':[6,7,5]}) #字典
c=pd.DataFrame(dict(a=[0,1,2],b=[3,4,5],c=[6,7,8])) #字典
#out: a b c
# 0 0 3 6
# 1 1 4 7
# 2 2 8 5
2 DataFrame属性
2.1 df.shape :查看DataFrame的形状
2.2 df.dtypes:返回DataFrame的列数据类型
2.3 df[df.index==某行索引值]:对某行内容进行索引
import pandas as pd
A=pd.DataFrame({'a':[1,2,3],'b':[2,3,4],'c':[2,4,5]})
print(A[A.index==0])
#Out:a b c
# 0 1 2 2
print(A[A.a==1])
#Out:a b c
# 0 1 2 2
2.4 df.columns:列索引
2.5 df.head():仅显示前面几行数据(默认是前五行)
2.6 df.tail():仅显示最后几行数据(默认是后五行)
2.7 dataframe.values:仅返回数据框中的值,轴标签将被删除
2.8 dataframe.astype: 对DataFrame的列数据类型转换
df = pd.DataFrame({'age': [ 3, 29],'height': [94, 170],'weight': [31, 115]})
#dfage height weight
0 3 94 31
1 29 170 115
#df.dtypes
age int64
height int64
weight int64
dtype: object
#df.values
array([[ 3, 94, 31],[ 29, 170, 115]])
3 DataFrame的索引
3.1 查看某列——df['列索引']
import pandas as pd
A=pd.DataFrame(dict(a=[1,2,3],b=[2,3,4],c=[23,5,2]))
print(A['a'])
#Out:0 1
# 1 2
# 2 3
# Name: a, dtype: int64
3.2 查看某个数据——df['列索引']['行索引']
import pandas as pd
import numpy as np
B=pd.DataFrame(np.arange(12).reshape(3,4),index=['a','b','c'],columns=['AA','BB','CC','DD'])
print(B['AA'][0])
#Out:0
需要注意的是想要查看某行,不能用df['行索引']这样的形式。因为在Pandas中方括号写数组,表示取行索引对行进行操作,方括号写字符串,表示取列索引对列进行操作。
3.2 查看某行——df.loc[]函数和df.iloc[]函数
import pandas as pd
import numpy as np
a=pd.DataFrame(np.arange(15).reshape(3,5),index=['a','b','c'],columns=['aa','bb','cc','dd','ee'])
print(a)
#Out1: aa bb cc dd ee
# a 0 1 2 3 4
# b 5 6 7 8 9
# c 10 11 12 13 14
print(a.loc['a':'b','aa':'dd'])
#Out2: aa bb cc dd
# a 0 1 2 3
# b 5 6 7 8
print(a.iloc[0:1,0:3])
#Out3: aa bb cc
# a 0 1 2
3.2.1 df.loc[索引]函数:方框+'索引',闭区间
3.2.2 df.iloc[下标]函数:方框+下标,左到右不到
在读取Series的数据时,就有通过方括号+索引/下标值的方式读取对应数据这两种方式。这是因为Series的索引是可以重新定义的,而下标始终是不变的(0开始)。
3.4 查看行数据时还可以以下方式
df[df.index==某一行索引值]:查看某行数据
df[df.某列索引名==该列的一个值]:对一行或多行内容进行索引
数据结构5 :DataFrame(Pandas库)相关推荐
- 第6章 数据结构化与数据存储(6.1数据结构化神器——pandas库,读取网页中的表格数据)
通过pandas库可以方便地爬取网页中的表格数据,对数据进行结构化处理,并导出为Excel工作簿等文件. 6.1.1用read_html()函数快速爬取网页表格数据 使用pandas库中的read_h ...
- 【Pandas库】(3) DataFrame的创建方法及基本操作
各位同学好,今天给大家介绍一下Pandas库中DataFrame类型数据的创建方法和基本操作. 文章内容如下: (1)使用字典类创建. 字典类有:①数组.列表.元组构成的字典:②Series构造的字典 ...
- Pandas库(1):DataFrame类
Pandas库围绕Series类型和DataFrame类型这两种数据结构,提供了一种高效便捷的数据处理方式. 一.DataFrame类概述 1.Series是pandas库的一维数据类型,DateFr ...
- python dataframe 新列_Python第二十四课:Pandas库(四)
Python第二十四课:Pandas库(四)点击上方"蓝字",关注我们. 不知不觉,我们已经跨越了千难万险,从零开始,一步步揭开了Python神秘的面纱.学到至今,回过头,才晓得自 ...
- python dataframe 列_python pandas库中DataFrame对行和列的操作实例讲解
用pandas中的DataFrame时选取行或列: import numpy as np import pandas as pd from pandas import Sereis, DataFram ...
- Pandas库DataFrame的简单应用2
Pandas练习2 练习题 DataFrame的简单应用 练习1 代码 效果图 练习2 代码 效果图 小结 练习题 DataFrame的简单应用 练习1 代码 import pandas as pd# ...
- pandas库随笔——更改Series与DataFrame的列标签
我们要对Series和DataFrame的列标签进行修改,通常是使用rename函数,还有一种方法就是使用暴力修改法,下面分别针对两种方法进行代码演示以及进行说明. 首先我们先载入pandas库并且生 ...
- pandas库--DataFrame常用操作
文章目录 前言 一.DataFrame创建 1.基于列表创建 2.基于字典创建 二.查询 1.df直接查询 ① 查询一列 ② 查询多列 ③ 条件查询 2.query()方法 ① 条件查询 ② 带有变量 ...
- python中不能使用索引运算的是_Python数据分析之Pandas库(笔记)
Pandas数据结构 pandas有两个基本的数据结构:Series和DataFrame. 1.1 创建Series数据 需要引入pandas模块:import pandas as pd 需要引入Se ...
- Py之Pandas:Python的pandas库简介、安装、使用方法详细攻略
Py之Pandas:Python的pandas库简介.安装.使用方法详细攻略 目录 pandas库简介 pandas库安装 pandas库使用方法 1.函数使用方法 2.使用经验总结 3.绘图相关操作 ...
最新文章
- 2022-2028年中国高等职业教育产业投资分析及前景预测报告
- Spring Cloud JWT文件生成
- jquery 添加 padding_【开发小技巧】11—如何使用JavaScript / jQuery为网站创夜间/高亮模式?...
- Oracle的LINUX安装
- 人工智能导论 王万良教授_学会动态丨辽宁省人工智能导论教学研讨活动在沈阳成功举办...
- 杭电2669拓展欧几里得
- curl参数为多维数组时提示数组到字符串的转换问题
- xps文档节点序列化,节点排序
- 8.Excel数据与指标概述
- 关于单片机串口发送和接收的问题
- IDEA Docker 插件安装
- E-MapReduce解决hive comment中文乱码问题
- 优雅的校验参数-javax.validation
- 《Beginning Linux Programming》读书笔记(一)
- SQL Server到底需要使用哪些端口
- 要关闭python解释器用什么快捷键_Python 解释器
- 1+5T LineageOS 谷歌相机 相关处理和链接
- springboot下载resources目录下的文件
- jQuery 选择器 _ 菜鸟教程
- ubuntu 16.04下chrome浏览器Flash过期解决方法
热门文章
- win 7 中的快速启动栏的设置
- 2014计算机论文,《2014计算机毕业论文.docx
- AVX指令去除-0符号位
- Eclipse无法加载插件解决办法
- 老虎证券赴美IPO:3年交易破万亿,5年9轮融资“众星捧月”...
- 2022-2028全球钢材防火涂料行业调研及趋势分析报告
- [乐意黎]phpQuery采集微信公众号文章乱码
- java合法标识符_JAVA合法标识符
- c语言中if(a字节4),【鲁班】的意思是什么?【鲁班】是什么意思?
- 微信小程序——背景音乐播放