码迷,mamicode.com
首页 > 其他好文 > 详细

Axis in DataFrame

时间:2020-04-02 22:15:27      阅读:69      评论:0      收藏:0      [点我收藏+]

标签:float   match   memory   cas   mem   series   tin   code   may   

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 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.

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
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.
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
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.
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
Just how to memory this? Exaggeratively imagine that, when DataFrame makes arithmetic operation with Series,the broadcasting direction is across index,downward,by default.And as for max(),mean(),the default set is axis=‘index‘,the finding direction is also across index,downward again. So why both are default to be downward?I do not know, or maybe just because of gravity.HaHa...

Axis in DataFrame

标签:float   match   memory   cas   mem   series   tin   code   may   

原文地址:https://www.cnblogs.com/johnyang/p/12623246.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!