Axis in DataFrame

Optional parameter axis may appear in arithmetric between DataFrame and Series,the key point understanding the meaning of axis is match,by default the index of series shall match columns of DataFrame,broadcasting down the rows;And axis may also appear in apply(),max(),mean() or so kind of DataFrame object method,by default, axis='index',meaning find the max one among index,and that is to find the max one of every column.Please note that,apply() is not identical to applymap().apply(f) will perform f function on one-dimentional array(index or columns),by default,axis='index' while applymap(f) will perform f function on element-wise for DataFrame.

import pandas as pd
import numpy as np
frame=pd.DataFrame(np.random.randn(4,3),index=['Utah','Ohio','Texas','Oregon'],columns=list('bde'));frame
.dataframe tbody tr th:only-of-type { vertical-align: middle }
\3c pre>\3c code>.dataframe tbody tr th { vertical-align: top }
.dataframe thead th { text-align: right }

b d e
Utah -0.311649 0.252285 -0.741715
Ohio 0.351583 1.287569 0.726872
Texas 0.605527 -0.186660 -0.993184
Oregon 1.577405 0.381833 1.607757
frame['b']
Utah     -0.311649
Ohio 0.351583
Texas 0.605527
Oregon 1.577405
Name: b, dtype: float64
series1=frame.iloc[0];series1
b   -0.311649
d 0.252285
e -0.741715
Name: Utah, dtype: float64
frame.sub(series1,axis='columns') # By default,arithmetic between DataFrame and Series matches the index of Series on the DataFrame's columns,broadcasting down the rows.
.dataframe tbody tr th:only-of-type { vertical-align: middle }
\3c pre>\3c code>.dataframe tbody tr th { vertical-align: top }
.dataframe thead th { text-align: right }

b d e
Utah 0.000000 0.000000 0.000000
Ohio 0.663232 1.035284 1.468587
Texas 0.917176 -0.438944 -0.251470
Oregon 1.889054 0.129548 2.349471
frame.sub(series1,axis=1) # The same with above
.dataframe tbody tr th:only-of-type { vertical-align: middle }
\3c pre>\3c code>.dataframe tbody tr th { vertical-align: top }
.dataframe thead th { text-align: right }

b d e
Utah 0.000000 0.000000 0.000000
Ohio 0.663232 1.035284 1.468587
Texas 0.917176 -0.438944 -0.251470
Oregon 1.889054 0.129548 2.349471
series2=frame['d'];series2
Utah      0.252285
Ohio 1.287569
Texas -0.186660
Oregon 0.381833
Name: d, dtype: float64
frame.sub(series2,axis='index') # Must set axis='index',so that broadcasts down on column.
.dataframe tbody tr th:only-of-type { vertical-align: middle }
\3c pre>\3c code>.dataframe tbody tr th { vertical-align: top }
.dataframe thead th { text-align: right }

b d e
Utah -0.563934 0.0 -0.993999
Ohio -0.935986 0.0 -0.560697
Texas 0.792186 0.0 -0.806525
Oregon 1.195572 0.0 1.225924
frame.max(axis='index') # max() default to set axis='index',meaning find the max one among 'index',not every max one of every index.
b    1.577405
d 1.287569
e 1.607757
dtype: float64

Summary:no matter arithmetic between df and series,and df object method, the operation steps can be divided into 2 setps,firstly, finding the direction of elementwise level operation,and then reapeating this process along the other direction.

df1=pd.DataFrame(np.arange(12).reshape(3,4));df1
0 1 2 3
0 0 1 2 3
1 4 5 6 7
2 8 9 10 11
df1.sub(df1.loc[1],axis=1)
0 1 2 3
0 -4 -4 -4 -4
1 0 0 0 0
2 4 4 4 4
df1.sub(df1[1],axis=0)
0 1 2 3
0 -1 0 1 2
1 -1 0 1 2
2 -1 0 1 2
df1.max(axis=0)
0     8
1 9
2 10
3 11
dtype: int32
df1.max(axis=1)
0     3
1 7
2 11
dtype: int32
The same rule can also be applied to np.concatenate() and pd.concat(),pd.DataFrame.any(),pd.DataFrame.all()

Signature: pd.concat(objs, axis=0, join='outer', join_axes=None, ignore_index=False, keys=None, levels=None, names=None, verify_integrity=False, copy=True)

Docstring:

Concatenate pandas objects along a particular axis with optional set logic

along the other axes. Can also add a layer of hierarchical indexing on the

concatenation axis, which may be useful if the labels are the same (or

overlapping) on the passed axis number

Parameters

objs : a sequence or mapping of Series, DataFrame, or Panel objects

If a dict is passed, the sorted keys will be used as the keys

argument, unless it is passed, in which case the values will be

selected (see below). Any None objects will be dropped silently unless

they are all None in which case a ValueError will be raised

axis : {0/'index', 1/'columns'}, default 0

The axis to concatenate along

join : {'inner', 'outer'}, default 'outer'

How to handle indexes on other axis(es)

join_axes : list of Index objects

Specific indexes to use for the other n - 1 axes instead of performing

inner/outer set logic

ignore_index : boolean, default False

If True, do not use the index values along the concatenation axis. The

resulting axis will be labeled 0, ..., n - 1. This is useful if you are

concatenating objects where the concatenation axis does not have

meaningful indexing information. Note the index values on the other

axes are still respected in the join.

keys : sequence, default None

If multiple levels passed, should contain tuples. Construct

hierarchical index using the passed keys as the outermost level

levels : list of sequences, default None

Specific levels (unique values) to use for constructing a

MultiIndex. Otherwise they will be inferred from the keys

names : list, default None

Names for the levels in the resulting hierarchical index

verify_integrity : boolean, default False

Check whether the new concatenated axis contains duplicates. This can

be very expensive relative to the actual data concatenation

copy : boolean, default True

If False, do not copy data unnecessarily

Notes

The keys, levels, and names arguments are all optional

Returns

concatenated : type of objects

File: e:\software\anaconda\lib\site-packages\pandas\tools\merge.py

Type: function

Axis in DataFrame的更多相关文章

  1. 数据类型-DataFrame

    数据类型-DataFrame DataFrame是由多个Series数据列组成的表格数据类型,每行Series值都增加了一个共用的索引 既有行索引,又有列索引 行索引,表明不同行,横向索引,叫inde ...

  2. pandas dataframe.apply() 实现对某一行/列进行处理获得一个新行/新列

    重点:dataframe.apply(function,axis)对一行或一列做出一些操作(axis=1则为对某一列进行操作,此时,apply函数每次将dataframe的一行传给function,然 ...

  3. Pandas 之 DataFrame 常用操作

    import numpy as np import pandas as pd This section will walk you(引导你) through the fundamental(基本的) ...

  4. [数据分析工具] Pandas 功能介绍(二)

    条件过滤 我们需要看第一季度的数据是怎样的,就需要使用条件过滤 体感的舒适适湿度是40-70,我们试着过滤出体感舒适湿度的数据 最后整合上面两种条件,在一季度体感湿度比较舒适的数据 列排序 数据按照某 ...

  5. 数据分析之pandas教程------数据处理

    目录 1  数据合并 1.1  实现数据库表join功能 1.2  实现union功能 2  数据转换 2.1  轴旋转 2.2  数据转换 2.2.1  去重 2.2.2  对某一列运用函数 2.2 ...

  6. 利用Python进行数据分析——pandas入门

    利用Python进行数据分析--pandas入门 基于NumPy建立的 from pandas importSeries,DataFrame,import pandas as pd 一.两种数据结构 ...

  7. pandas知识点

    1.选择对象 1.选择特定列和行的数据 a['x'] 那么将会返回columns为x的列,注意这种方式一次只能返回一个列.a.x与a['x']意思一样. 取行数据,通过切片[]来选择 如:a[0:3] ...

  8. python数据分析及展示(三)

    一.Pandas库入门 1. Pandas库的介绍 http://pandas.pydata.org Pandas是Python第三方库,提供高性能易用数据类型和分析工具 import pandas ...

  9. pandas 基础操作 更新

    创建一个Series,同时让pandas自动生成索引列 创建一个DataFrame数据框 查看数据 数据的简单统计 数据的排序 选择数据(类似于数据库中sql语句) 另外可以使用标签来选择 通过位置获 ...

  10. pandas 数据结构基础与转换

    pandas 最常用的三种基本数据结构: 1.dataFrame: https://pandas.pydata.org/pandas-docs/stable/generated/pandas.Data ...

随机推荐

  1. Python - qrcode(二维码模块)

    import qrcode codeText = 'https://www.cnblogs.com/houhuilinblogs' img = qrcode.make(codeText) print( ...

  2. [rustGUI][iced]基于rust的GUI库iced(0.13)的部件学习(06):基于iced实现一个简单的图片浏览器

    前言 本文是关于iced库的部件介绍,iced库是基于rust的GUI库,作者自述是受Elm启发. iced目前的版本是0.13.1,相较于此前的0.12版本,有较大改动. 本合集是基于新版本的关于分 ...

  3. 961. 重复 N 次的元素

    地址:https://leetcode-cn.com/problems/n-repeated-element-in-size-2n-array/ <?php /** 在大小为 2N 的数组 A ...

  4. html5文本标签

    标题文本 h1.h2.h3.h4.h5.h6 其中 h1.h2.h3是比较常用的.h3.h4.h5.h6相对来说用的会少一点,除非结构层次比较深才会使用. 段落文本 p <p>这是一个段落 ...

  5. [tldr] vscode的remote插件的config文件内容解析

    参考VS Code Remote SSH配置 解决了什么问题 vscode的remote插件可以直接通过可视化的UI新建一个连接 通过ssh指令添加服务器的连接方式 但是这种方式添加的服务器名字等于服 ...

  6. 魔方求解器程序(层先法,java版本)

    实现了一个三阶魔方的层先法求解程序:https://github.com/davelet/java-puzzle-resolver 欢迎试用. 用法 1. 随机试用 不关注起始状态的话可以用程序的随机 ...

  7. Bash Shell 30min 过家家

    带你捅破窗户纸 - 备注 : @博客园 : 1. 为什么不支持 pdf 上传了呀 2. 网站分类不好用 3. 排版OA工具升级下, 例如 markdown 写出来好丑. 尝试升级下呢 ? 后记: 学如 ...

  8. PDF转换:从Word到Excel

    一.引言 在数字化的浪潮中,PDF文件格式以其稳定性和兼容性成为了信息交流的宠儿.然而,当我们需要编辑这些PDF文件时,往往会遇到各种难题.今天,我要和大家分享的,是如何将PDF文件轻松转换成Word ...

  9. NextJS CVE-2025-29927 安全漏洞

    NextJS CVE-2025-29927 安全漏洞 CVE-2025-29927 是一个存在于 Next.js 框架中的关键安全漏洞.该漏洞允许攻击者通过伪造或篡改 x-middleware-sub ...

  10. 关于TFDMemtable的使用场景【2】处理SOAP/REST取得的数据

    如果可以直接获得JSON数据,那么可以直接连到TFDMemtable进行显示和编辑. 1.一组REST组件.RESTClient的属性BaseURL是http地址. 2.点击TRESTRequest右 ...