Axis in DataFrame

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() or so function,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
<style scoped=""> .dataframe tbody tr th:only-of-type { vertical-align: middle; }
.dataframe tbody tr th {
    vertical-align: top;
}

.dataframe thead th {
    text-align: right;
}
</style>
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.
<style scoped=""> .dataframe tbody tr th:only-of-type { vertical-align: middle; }
.dataframe tbody tr th {
    vertical-align: top;
}

.dataframe thead th {
    text-align: right;
}
</style>
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
<style scoped=""> .dataframe tbody tr th:only-of-type { vertical-align: middle; }
.dataframe tbody tr th {
    vertical-align: top;
}

.dataframe thead th {
    text-align: right;
}
</style>
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.
<style scoped=""> .dataframe tbody tr th:only-of-type { vertical-align: middle; }
.dataframe tbody tr th {
    vertical-align: top;
}

.dataframe thead th {
    text-align: right;
}
</style>
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

上一篇:寒假CS每日打卡 Feb.17th


下一篇:2020 BIT冬训-二分三分快速幂矩阵 C - Multiplication Table CodeForces - 448D