【01】DataFrame的创建和属性
DataFrame是一个表格型的数据结构,可以看成就是excel中的表格。
官方文档:https://pandas.pydata.org/docs/reference/frame.html
DataFrame的创建
DataFrame构造方法如下:
pandas.DataFrame(data=None, index=None, columns=None, dtype=None, copy=False)
- data:DataFrame的数据部分,可以是字典、二维数组、Series、DataFrame或其他可转换为DataFrame的对象,若不提供此参数,则创建一个空的DataFrame。
- index:DataFrame的行索引,用于标识每行数据,可以是列表、数组、索引对象等,若不提供此参数,则创建一个默认的整数索引。
- columns:DataFrame的列索引,用于标识每列数据。可以是列表、数组、索引对象等,若不提供此参数,则创建默认的整数索引。
- dtype:指定DataFrame的数据类型,可以是NumPy的数据类型,例如np.int64、np.float64等,若不提供此参数,则根据数据自动推断数据类型。
- copy:是否复制数据,默认为False,表示不复制数据,若设置为True,则复制输入的数据。
一维列表创建DataFrame
|
![]() |
二维列表创建DataFrame
|
![]() |
传递字典创建DataFrame
|
![]() |
传递字典列表创建DataFrame
|
![]() |
通过Series对象创建
|
![]() |
通过Numpy创建
|
![]() |
DataFrame的属性
dataframe.T
df.T属性主要用来转置行和列,和 df.transpose() 实现的效果一样。
|
![]() |
dataframe.axes
返回包含行索引和列索引的列表,可以通过 df.axes[0].tolist() 或 list(df.axes[0]) 转成行索引列表,列索引列表同理。
import pandas as pd
import numpy as np
a = np.random.randint(1, 10, (3, 2))
# 基于a数组建立DataFrame
df = pd.DataFrame(a, columns=['foo', 'bar'], index=['a', 'b', 'c'])
print(df)
print("*" * 50)
print(df.axes) # [Index(['a', 'b', 'c'], dtype='object'), Index(['foo', 'bar'], dtype='object')]
print(df.axes[0].tolist()) # ['a', 'b', 'c']
print(list(df.axes[0])) # ['a', 'b', 'c']
dataframe.dtypes
查看每列的数据类型。
|
![]() |
dataframe.ndim
获取DataFrame的维数。
import pandas as pd
import numpy as np
a = np.random.randint(1, 10, (3, 2))
# 基于a数组建立DataFrame
df = pd.DataFrame(a, columns=['foo', 'bar'], index=['a', 'b', 'c'])
print(df)
print("*" * 50)
print(df.ndim) # 2
dataframe.shape
获取DataFrame的行数和列数,是一个元组。
import pandas as pd
import numpy as np
a = np.random.randint(1, 10, (3, 2))
# 基于a数组建立DataFrame
df = pd.DataFrame(a, columns=['foo', 'bar'], index=['a', 'b', 'c'])
print(df)
print("*" * 50)
print(df.shape) # (3, 2)
dataframe.size
返回DataFrame中的元素个数。
import pandas as pd
import numpy as np
a = np.random.randint(1, 10, (3, 2))
# 基于a数组建立DataFrame
df = pd.DataFrame(a, columns=['foo', 'bar'], index=['a', 'b', 'c'])
print(df)
print("*" * 50)
print(df.size) # 6
dataframe.values
返回一个所有行数据组成的二维的数组,每个元素是一个一维数组(也就是一行数据),可以通过 list(df.values) 或 df.values.tolist() 转成python的列表类型。
import pandas as pd
import numpy as np
a = np.random.randint(1, 10, (3, 2))
# 基于a数组建立DataFrame
df = pd.DataFrame(a, columns=['foo', 'bar'], index=['a', 'b', 'c'])
print(df)
print("*" * 50)
print(df.values) # [[8 6] [3 3] [8 7]]
print(list(df.values)) # [array([8, 6], dtype=int32), array([3, 3], dtype=int32), array([8, 7], dtype=int32)]
print(df.values.tolist()) # [[8, 6], [3, 3], [8, 7]]
dataframe.index
获取行索引,返回的是Index类型,可以通过 list(df.index) 或 df.index.tolist() 转换成列表。
import pandas as pd
import numpy as np
a = np.random.randint(1, 10, (3, 2))
df = pd.DataFrame(a, columns=['foo', 'bar'], index=['a', 'b', 'c'])
print(df)
print("*" * 50)
print(df.index) # Index(['a', 'b', 'c'], dtype='object')
print(df.index.values) # ['a' 'b' 'c']
print(list(df.index)) # ['a', 'b', 'c']
print(df.index.tolist()) # ['a', 'b', 'c']
dataframe.columns
获取列索引,返回的是Index类型,可以通过 list(df.columns) 或 df.columns.tolist() 转换成列表。
import pandas as pd
import numpy as np
a = np.random.randint(1, 10, (3, 2))
df = pd.DataFrame(a, columns=['foo', 'bar'], index=['a', 'b', 'c'])
print(df)
print("*" * 50)
print(df.columns) # Index(['foo', 'bar'], dtype='object')
print(df.columns.values) # ['foo' 'bar'],可用 df.columns.values.tolist() 转换成列表
print(list(df.columns)) # ['foo', 'bar']
print(df.columns.tolist()) # ['foo', 'bar']
【01】DataFrame的创建和属性的更多相关文章
- Pandas的基础操作(一)——矩阵表的创建及其属性
Pandas的基础操作(一)——矩阵表的创建及其属性 (注:记得在文件开头导入import numpy as np以及import pandas as pd) import pandas as pd ...
- 继承自UITableView的类自带tableView属性,不需要在创建该属性,因为父类UITableView已经创建.
继承自UITableView的类自带tableView属性,不需要在创建该属性,因为父类UITableView已经创建. https://www.evernote.com/shard/s227 ...
- JS对象—数组总结(创建、属性、方法)
JS对象—数组总结(创建.属性.方法) 1.创建字符串 1.1 new Array() var arr1 = new Array(); var arr2 = new Array(6); 数组的长度为6 ...
- Python 中使用动态创建类属性的机制实现接口之后的依赖
我们在自动化测试中经常会需要关联用例处理,需要动态类属性: 推荐使用第二种方法: 创建:setattr() 获取:getattr() 两种,如何创建 类属性 loan_id # 第一种,创建 # 类名 ...
- DataFrame的创建
DataFrame的创建从Spark2.0以上版本开始,Spark使用全新的SparkSession接口替代Spark1.6中的SQLContext及HiveContext接口来实现其对数据加载.转换 ...
- 原创:MVC 5 实例教程(MvcMovieStore 新概念版:mvc5.0,EF6.01) - 4、创建数据上下文和数据实体模型
说明:MvcMovieStore项目已经发布上线,想了解最新版本功能请登录 MVC影视(MvcMovie.cn) 进行查阅.如需转载,请注明出处:http://www.cnblogs.com/Dodu ...
- 大数据学习day24-------spark07-----1. sortBy是Transformation算子,为什么会触发Action 2. SparkSQL 3. DataFrame的创建 4. DSL风格API语法 5 两种风格(SQL、DSL)计算workcount案例
1. sortBy是Transformation算子,为什么会触发Action sortBy需要对数据进行全局排序,其需要用到RangePartitioner,而在创建RangePartitioner ...
- CSS.01 -- 选择器及相关的属性文本、文字、字体、颜色、
与html相比,Css支持更丰富的文档外观,Css可以为任何元素的文本和背景设置颜色:允许在任何元素外围设置边框:允许改变文本的大小,装饰(如下划线),间隔,甚至可以确定是否显示文本. 什么是CSS? ...
- python中创建实例属性
虽然可以通过Person类创建出xiaoming.xiaohong等实例,但是这些实例看上除了地址不同外,没有什么其他不同.在现实世界中,区分xiaoming.xiaohong要依靠他们各自的名字.性 ...
- spark DataFrame的创建几种方式和存储
一. 从Spark2.0以上版本开始,Spark使用全新的SparkSession接口替代Spark1.6中的SQLContext及HiveContext接口来实现其对数据加载.转换.处理等功能.Sp ...
随机推荐
- 【H5】11 表格
概述: 在HTML中一个很普通的任务是构建表格数据,有大量的元素和属性是来满足这种需求的. 只需要一点儿的CSS来设定风格,HTML让在web上显示表格数据变的很容易,例如你的学校的教学计划,你当地的 ...
- 【SQL】列转字符串函数
还是这个需求 主界面的列表表格是直接在后台用SQL查出来的 public String getQuerySql(ElemBean condition, List<Object> param ...
- linux测试cpu性能的命令
linux测试cpu性能的命令 在Linux中,可以使用多种命令来测试CPU性能.以下是一些常用的命令: stress: 一个通用的压力测试工具,可以生成CPU.内存.IO等负载. 安装: sudo ...
- 小米(xiaomi)自动驾驶技术的原始技术积累 —— CyberDog 仿生四足机器狗
相关: https://www.youtube.com/watch?v=f0q8tfZ89Qo 小米公司一直没有加入到制造电动车的行列中,直到几年前才感觉造车是必须要走的路了,但是造车就一定是要造电动 ...
- 中国特供阉割版4090D建议安装最新驱动,据说不然的话会报error:4090和4090D对比
资料来源: https://www.bilibili.com/video/BV1oa4y127fG/?spm_id_from=333.999.0.0&vd_source=f1d0f27367a ...
- A3C与GA3C的收敛性分析
G-A3C的代码: https://gitee.com/devilmaycry812839668/gpu_a3c 论文: <Reinforcement Learning thorugh Asyn ...
- 关于python:pip安装选项“ ignore-installed”和“ force-reinstall”之间的区别
参考: https://www.codenong.com/51913361/ ==================================================== 官方文档解释: ...
- 再探 游戏 《 2048 》 —— AI方法—— 缘起、缘灭(7) —— Python版本实现的《2048》游戏的TDL算法
<2048>游戏在线试玩地址: https://play2048.co/ 如何解决<2048>游戏源于外网的一个讨论帖子,而这个帖子则是讨论如何解决该游戏的最早开始,可谓是&q ...
- 白鲸开源CEO郭炜荣获「2024中国数智化转型升级先锋人物」称号
2024年7月24日,由数据猿主办,IDC协办,新华社中国经济信息社.上海大数据联盟.上海市数商协会.上海超级计算中心作为支持单位,举办"数智新质·力拓未来 2024企业数智化转型升级发展论 ...
- 聊一聊SQL优化
晚上睡不着,脑子里总想着一些问题,试着写一写对于SQL查询优化的见解. 首先,数据库有自己的查询优化器,执行一条查询SQL优化器会选择最优的方式(不走索引.走索引.走哪个索引), 所以索引不是越多越好 ...







