Python 中 axis = 0 和 axis = 1 的用法?2025年1月5日 | 阅读 7 分钟 引言Pandas 库是 Python 中一个强大的数据处理工具,对于许多数据分析师、科学家和工程师来说,它已成为不可或缺的一部分。'axis' 参数是 Pandas 的一个重要特性,它允许执行跨不同轴的操作。在本详细指南中,我们将讨论使用 'axis=0' 和 'axis=1' 的复杂性,揭示它们的importance以及在实践中如何使用的见解。 理解基础知识在深入了解 **'axis=0' 和 'axis=1'** 的细节之前,了解 Pandas 中轴的含义非常重要。Pandas DataFrame 是一个带有标签的二维数据结构,包含行和列。**行和列都用标签进行索引,Pandas 提供了两个轴:行轴 (axis=0) 和列轴 (axis=1)**。 1. 'axis=0' - 沿行的操作参数 **'axis = 0' 用于定义 DataFrame 的行应执行某个操作**。这意味着操作将垂直向下进行。沿 'axis=0' 的一些常见操作是聚合函数,如 sum、mean 或统计函数。 让我们举一个例子来展示 axis=0 的用法。假设我们有一个 DataFrame 'df'。 输出 Subject Means (axis=0): Math 84.333333 English 91.666667 Science 87.000000 dtype: float64 <ipython-input-2-c74a6ec0dacd>:12: FutureWarning: The default value of numeric_only in DataFrame.mean is deprecated. In a future version, it will default to False. In addition, specifying 'numeric_only=None' is deprecated. Select only valid columns or specify the value of numeric_only to silence this warning. subject_means = df.mean(axis=0). 说明
2. axis=1 - 沿列的操作相反,**'axis=1' 选项用于操作 DataFrame 的列**。更具体地说,这意味着在列之间水平应用操作。沿 axis=1 的一些常见操作是连接列、对每一行执行函数或基于当前列创建新列。 为了说明 'axis=1',我们可以继续前面的例子。假设我们想创建一个新列,代表每个学生获得的总分数。 输出 DataFrame with Total Marks (axis=1): Name Math English Science Total 0 John 85 92 89 266 1 Alex 90 88 84 262 2 Barney 78 95 88 261 <ipython-input-3-24335492b0be>:2: FutureWarning: Dropping of nuisance columns in DataFrame reductions (with 'numeric_only=None') is deprecated; in a future version this will raise TypeError. Select only valid columns before calling the reduction. df['Total'] = df.sum(axis=1) 说明 在此实例中,'axis=1' 确保沿着每一行计算总和,从而得到一个名为 Total 的新列,其中包含所有学生的总分数。 实际应用现在是时候考虑一些需要同时考虑 'axis=0' 和 'axis=1' 参数的实际用例了。 1. 统计分析使用大型数据集时,统计分析是一项常见任务。使用 'axis=0',可以轻松地描述每列的统计数据,例如平均值、中位数和标准差,以了解任何特征的分布模式。 输出 Column-wise Descriptive Statistics: <bound method NDFrame.describe of Name Math English Science Total 0 John 85 92 89 266 1 Alex 90 88 84 262 2 Barney 78 95 88 261> 说明 当与 axis=0 一起使用 describe 函数时,它会为每一列生成详细摘要,表示其数值特征。 2. 行式操作当您需要单独对每一行执行操作时,**'axis=1'** 就变得至关重要。例如,假设您想计算每个学生在每个科目中相对于最高分数的百分比。 输出 Percentage Marks (axis=1): Math English Science 0 0.85 0.92 0.89 1 0.90 0.88 0.84 2 0.78 0.95 0.88 说明
3. 数据清理和转换数据清理通常包括处理缺失值或将现有列转换为更合适的格式。Axis=0 帮助您沿列识别和管理缺失值,而 axis=1 则允许进行逐行转换。 输出 DataFrame after Dropping Columns with Missing Values (axis=1): Name Math English Science 0 John 85 92 89 1 Alex 90 88 84 2 Barney 78 95 88 说明
4. 连接和合并连接或合并多个 DataFrame 需要 'axis=0' 和 'axis=1'。当沿着行堆叠时,'axis=0' 表示垂直对齐,而连接列需要指定 'axis = 1' 进行水平方向。 输出 Concatenated DataFrames along Rows (axis=0): A B 0 1 3 1 2 4 0 5 7 1 6 8 Concatenated DataFrames along Columns (axis=1): A B A B 1 3 5 7 2 4 6 8 说明
高级技术通过处理 Pandas 的经验,您可能会遇到需要依赖 'axis=0' 和 'axis = 1' 的高级方法的情况。让我们探讨一些这样的技术。 1. 分组和聚合当您想根据特定标准汇总数据时,聚合函数与 **'groupby'** 操作一起非常有用。Axis 参数可以帮助确定聚合是针对行还是列执行的。 输出 GroupBy and Aggregation (axis=0): Math English Science Total Category Group1 81.5 93.5 88.5 263.5 Group2 90.0 88.0 84.0 262.0 <ipython-input-13-1fa7fa67a9b3>:3: FutureWarning: The default value of numeric_only in DataFrameGroupBy.mean is deprecated. In a future version, numeric_only will default to False. Either specify numeric_only or select only columns that should be valid for the function. category_means = df.groupby('Category').mean() 说明
2. 转置和重塑Pandas 中的 T 属性允许您转置 DataFrame,交换行和列。此转换会交换 axis=0 和 axis = 1,反之亦然。 当原始方向不允许正确分析和可视化时,转置 DataFrame 可能很有帮助。 输出 Transposed DataFrame: 0 1 2 Name John Alex Barney Math 85 90 78 English 92 88 95 Science 89 84 88 Total 266 262 261 Category Group1 Group2 Group1 说明
结论在本篇关于 Pandas 的详细指南中,我们讨论了使用 'axis=0' 和 'axis = 1' 的复杂性,它们的重要性以及实际操作。当执行统计分析、清理和转换数据或在使用 GroupBy 和聚合等复杂技术处理数据时,这些参数对您来说尤其重要。随着您继续使用 Pandas,请尝试不同的场景,并学习如何应用 'axis = 0' 和 'axis=1',这将使您未来的数据分析更加出色。 |
我们请求您订阅我们的新闻通讯以获取最新更新。