pandas.DataFrame 基本技巧head / tail / .T:数据查看、转置del / drop:删除对齐排序.sort_values:按值排序.sort_index:索引排序 head / tail / .T:数据查看、转置 import numpy import pandas df = pandas.DataFrame(numpy.random.rand(16).reshape(8,2), columns = ["a","b"]) # 默认查看5条 print(df.head(2)) # .head()查看头部数据 print(df.tail()) # .tail()查看尾 在Python数据分析领域,pandas库中的DataFrame是处理二维表格数据的核心工具。本篇文章将深入探讨DataFrame的一些基本技巧,包括数据查看、转置、删除、对齐排序等操作。 我们来看数据查看的方法。`head()`函数用于查看DataFrame的前几行,默认显示前5行,可以通过参数指定显示行数,例如`df.head(2)`会显示前2行。而`tail()`函数则用于查看DataFrame的最后几行,没有指定参数时默认显示最后5行。`.T`属性则是用来转置DataFrame,即交换行和列,使行变为列,列变为行。 接下来是删除操作。使用`del`关键字可以直接删除DataFrame中的列,如`del df["a"]`会删除名为"a"的列。而`drop()`函数则更加灵活,可以删除行或列。例如,`df.drop(0)`会删除第一行,`df.drop(["d"], axis=1)`会删除名为"d"的列。注意,`drop()`函数的`inplace`参数,如果设置为`True`,操作会直接在原始DataFrame上进行,否则会返回一个新的DataFrame。 在DataFrame的对齐操作中,pandas会自动根据列名和索引进行对齐。当你将两个DataFrame相加或进行其他运算时,它们会自动调整大小和形状,以确保对应位置的数据进行操作。例如,`df1 + df2`会尝试将df2的列与df1对齐,如果列名相同则进行相加,否则结果中会出现NaN值。 排序是数据分析中常见的需求。`sort_values()`函数按DataFrame的值进行排序,可以对单列或多列进行操作。例如,`df.sort_values('a', ascending=False)`会按'a'列的值降序排列。`sort_index()`函数则是按DataFrame的索引进行排序,同样支持升序和降序,例如`df.sort_index(ascending=False)`。 pandas.DataFrame提供了丰富的功能来方便用户处理和分析数据。这些基本技巧是数据分析的基础,掌握它们能帮助你更高效地探索和理解数据。无论是查看数据、进行数据清理还是进行复杂的数据操作,pandas DataFrame都能提供强大的支持。在实际工作中,结合numpy和其他相关库,你可以构建出强大的数据分析流程。
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![thumb](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![avatar](https://profile-avatar.csdnimg.cn/default.jpg!1)
- 粉丝: 2
- 资源: 843
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![voice](https://csdnimg.cn/release/downloadcmsfe/public/img/voice.245cc511.png)
![center-task](https://csdnimg.cn/release/downloadcmsfe/public/img/center-task.c2eda91a.png)
最新资源
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback-tip](https://img-home.csdnimg.cn/images/20220527035111.png)
![dialog-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/green-success.6a4acb44.png)
- 1
- 2
前往页