使用轴

时间:2017-05-26 13:21:31

标签: python pandas numpy

我正在使用python阅读数据分析。我有以下代码。据我所知,轴0表示行方式,轴1表示列方式。如果我们传递轴0

,我们正在下面列出
>>> frame
        b   d   e
Utah    0   1   2
Ohio    3   4   5
Texas   6   7   8
Oregon  9  10  11

>>> series = frame['d']
>>> series
Utah       1
Ohio       4
Texas      7
Oregon    10
Name: d, dtype: int32
>>> 
>>> 
>>> frame.sub(series, axis=0)
        b  d  e
Utah   -1  0  1
Ohio   -1  0  1
Texas  -1  0  1
Oregon -1  0  1

Anothere问题

In [158]: frame = DataFrame(np.random.randn(4, 3), columns=list('bde'),
                        .....: index=['Utah', 'Ohio', 'Texas', 'Oregon'])
In [159]: frame In [160]: np.abs(frame)
Out[159]: Out[160]:
           b       d        e                 b        d        e
Utah -0.204708 0.478943 -0.519439    Utah 0.204708 0.478943 0.519439
Ohio -0.555730 1.965781 1.393406     Ohio 0.555730 1.965781 1.393406
Texas 0.092908 0.281746 0.769023     Texas 0.092908 0.281746 0.769023
Oregon 1.246435 1.007189 -1.296221   Oregon 1.246435 1.007189 1.296221

另一个常见的操作是将1D数组上的函数应用于每个列或行.DataFrame的apply方法正是这样:

In [161]: f = lambda x: x.max() - x.min()
In [162]: frame.apply(f) In [163]: frame.apply(f, axis=1)
Out[162]:                 Out[163]:
b 1.802165                Utah 0.998382
d 1.684034                Ohio 2.521511
e 2.689627                Texas 0.676115
Oregon 2.542656

这里我们也指定了默认行的轴0,但这里我们按列计算函数。我很困惑请解释一下。

1 个答案:

答案 0 :(得分:1)

如果选中DataFrame.sub(同时addmuldiv):

  

:{0,1,'index','columns'}
  对于系列输入,轴匹配

上的系列索引

检查此样本:

print (frame)
       b   d   e
Utah    0   1   2
Ohio    3   4   5
Texas   6   7   8
Oregon  9  10  11

series1 = frame['d']
series2 = frame.loc['Texas']

#series is matched index - axis=0
print (frame.sub(series1, axis=0))
        b  d  e
Utah   -1  0  1
Ohio   -1  0  1
Texas  -1  0  1
Oregon -1  0  1

#series is matched columns - axis=1
print (frame.sub(series2, axis=1))
        b  d  e
Utah   -6 -6 -6
Ohio   -3 -3 -3
Texas   0  0  0
Oregon  3  3  3

您还可以查看matching-broadcasting-behaviorthis very nice explanation of broadcasting