numpy库与pandas库axis=0,axis= 1轴的用法详解


Posted in Python onMay 27, 2019

对数据进行操作时,经常需要在横轴方向或者数轴方向对数据进行操作,这时需要设定参数axis的值:

  • axis = 0 代表对横轴操作,也就是第0轴;
  • axis = 1 代表对纵轴操作,也就是第1轴;

numpy库中横轴、纵轴 axis 参数实例详解:

In [1]: import numpy as np
#生成一个3行4列的数组
In [2]: a = np.arange(12).reshape(3,4)
In [3]: a
Out[3]:
array([[ 0, 1, 2, 3],
    [ 4, 5, 6, 7],
    [ 8, 9, 10, 11]])
#axis= 0 对a的横轴进行操作,在运算的过程中其运算的方向表现为纵向运算
In [4]: a.sum(axis = 0)
Out[4]: array([12, 15, 18, 21])
#axis= 1 对a的纵轴进行操作,在运算的过程中其运算的方向表现为横向运算
In [5]: a.sum(axis = 1)
Out[5]: array([ 6, 22, 38])

pandas库DataFrame中横轴、纵轴 axis 参数实例详解:

In [8]: b = pd.DataFrame(np.arange(24).reshape(4,6))
In [9]: b
Out[9]:
  0  1  2  3  4  5
0  0  1  2  3  4  5
1  6  7  8  9 10 11
2 12 13 14 15 16 17
3 18 19 20 21 22 23
#axis= 0 对b的横轴进行操作,在运算的过程中其运算的方向表现为纵向运算
In [10]: b.sum(axis = 0)
Out[10]:
0  36
1  40
2  44
3  48
4  52
5  56
dtype: int64
#axis= 1 对b的横轴进行操作,在运算的过程中其运算的方向表现为纵向运算
In [11]: b.sum(axis = 1)
Out[11]:
0   15
1   51
2   87
3  123
dtype: int64

numpy库与pandas库axis=0,axis= 1轴的用法详解

pandas库panel中axis 参数实例详解:

In [18]: np.arange(24).reshape(2,3,4)
Out[18]:
array([[[ 0, 1, 2, 3],
    [ 4, 5, 6, 7],
    [ 8, 9, 10, 11]],
 
    [[12, 13, 14, 15],
    [16, 17, 18, 19],
    [20, 21, 22, 23]]])
#生成面板数据
In [19]: c = pd.Panel(np.arange(24).reshape(2,3,4))
In [24]: c
Out[24]:
<class 'pandas.core.panel.Panel'>
Dimensions: 2 (items) x 3 (major_axis) x 4 (minor_axis)
Items axis: 0 to 1
Major_axis axis: 0 to 2
Minor_axis axis: 0 to 3
#对Items axis轴的数据进行操作,也就是panel里面的0轴:
In [20]: c.sum(axis = 0)
Out[20]:
  0  1  2  3
0 12 14 16 18
1 20 22 24 26
2 28 30 32 34
对Major_axis axis轴的数据进行操作
In [21]: c.sum(axis = 1)
Out[21]:
  0  1
0 12 48
1 15 51
2 18 54
3 21 57
对Minor_axis axis轴的数据进行操作
In [22]: c.sum(axis = 2)
Out[22]:
  0  1
0  6 54
1 22 70
2 38 86

numpy库与pandas库axis=0,axis= 1轴的用法详解

如果是2维数组,先横轴后纵轴;如果是3维数组,先最外层,然后一层一层按照先横轴再纵轴的逻辑进行匹配轴。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python 匹配任意字符(包括换行符)的正则表达式写法
Oct 29 Python
Python的高级Git库 Gittle
Sep 22 Python
在Linux中通过Python脚本访问mdb数据库的方法
May 06 Python
python实现基本进制转换的方法
Jul 11 Python
Python中的os.path路径模块中的操作方法总结
Jul 07 Python
详解Python 实现元胞自动机中的生命游戏(Game of life)
Jan 27 Python
用Python写一个模拟qq聊天小程序的代码实例
Mar 06 Python
Python2与Python3的区别实例分析
Apr 11 Python
利用python和百度地图API实现数据地图标注的方法
May 13 Python
Python实现数值积分方式
Nov 20 Python
Python获取百度热搜的完整代码
Apr 07 Python
Python实现数据的序列化操作详解
Jul 07 Python
Python之NumPy(axis=0 与axis=1)区分详解
May 27 #Python
Python3.7 新特性之dataclass装饰器
May 27 #Python
Python3多目标赋值及共享引用注意事项
May 27 #Python
Python中字符串String的基本内置函数与过滤字符模块函数的基本用法
May 27 #Python
python占位符输入方式实例
May 27 #Python
numpy.where() 用法详解
May 27 #Python
python numpy实现文件存取的示例代码
May 26 #Python
You might like
Apache, PHP在Windows 9x/NT下的安装与配置 (一)
2006/10/09 PHP
php ignore_user_abort与register_shutdown_function 使用方法
2009/06/14 PHP
php+jquery编码方面的一些心得(utf-8 gb2312)
2010/10/12 PHP
laravel创建类似ThinPHP中functions.php的全局函数
2016/11/26 PHP
PJBlog插件 防刷新的在线播放器
2006/10/25 Javascript
javascript document.compatMode兼容性
2010/02/23 Javascript
JavaScript高级程序设计 阅读笔记(十二) js内置对象Math
2012/08/14 Javascript
jquery选择checked在ie8普通模式下的问题
2014/02/12 Javascript
js交换排序 冒泡排序算法(Javascript版)
2014/10/04 Javascript
node.js中的events.EventEmitter.listenerCount方法使用说明
2014/12/08 Javascript
jQuery插件开发的五种形态小结
2015/03/04 Javascript
js确认框confirm()用法实例详解
2016/01/07 Javascript
jquery制做精致的倒计时特效
2016/06/13 Javascript
js实现图片上传预览原理分析
2017/07/13 Javascript
webpack v4 从dev到prd的方法
2018/04/02 Javascript
为什么使用koa2搭建微信第三方公众平台的原因
2018/05/16 Javascript
微信小程序实现自定义picker选择器弹窗内容
2020/05/26 Javascript
vue单文件组件lint error自动fix与styleLint报错自动fix详解
2019/01/08 Javascript
通过说明与示例了解js五种设计模式
2019/06/17 Javascript
[01:45]DOTA2众星出演!DSPL刀塔次级职业联赛宣传片
2014/11/21 DOTA
Python 文件操作实现代码
2009/10/07 Python
Python面向对象类编写细节分析【类,方法,继承,超类,接口等】
2019/01/05 Python
python实现猜拳小游戏
2020/04/05 Python
Python基于gevent实现文件字符串查找器
2020/08/11 Python
Matplotlib配色之Colormap详解
2021/01/05 Python
用html5的canvas画布绘制贝塞尔曲线完整代码
2013/08/14 HTML / CSS
马来西亚网上美容店:Hermo.my
2017/11/25 全球购物
香港现代设计家具品牌:Ziinlife Furniture
2018/11/13 全球购物
出国留学担保书
2014/05/20 职场文书
学校2014重阳节活动策划方案
2014/09/16 职场文书
2014年部门工作总结
2014/11/12 职场文书
2015年五一劳动节演讲稿
2015/03/18 职场文书
聋哑人盗窃罪辩护词
2015/05/21 职场文书
2015年小学生暑假总结
2015/07/13 职场文书
法制工作总结2015
2015/07/23 职场文书
七年级英语教学反思
2016/02/15 职场文书