Numpy 中的通用函數(shù)方法(逐元素數(shù)組方法)對 pandas 也有效
In [54]: frame = pd.DataFrame(np.random.randn(4, 3), columns=list('bde'),
...: index=['Utah', 'Ohio', 'Texas', 'Oregon'])
In [55]: frame
Out[55]:
b d e
Utah 0.053371 -2.529817 0.368989
Ohio -1.696226 -0.244630 -0.470242
Texas 2.574698 -2.013593 -0.644810
Oregon -0.739534 0.488870 1.704059
In [56]: np.abs(frame)
Out[56]:
b d e
Utah 0.053371 2.529817 0.368989
Ohio 1.696226 0.244630 0.470242
Texas 2.574698 2.013593 0.644810
Oregon 0.739534 0.488870 1.704059
將一個函數(shù)用到一行或一列一維數(shù)組上
In [57]: f = lambda x: x.max() - x.min()
In [58]: frame.apply(f)
Out[58]:
b 4.270924
d 3.018687
e 2.348869
dtype: float64
每一列調(diào)用一次
In [59]: frame.apply(f, axis='columns')
Out[59]:
Utah 2.898806
Ohio 1.451596
Texas 4.588292
Oregon 2.443593
dtype: float64
每一行調(diào)用一次
In [60]: def f(x):
...: return pd.Series([x.min(), x.max()], index=['min', 'max'])
...:
In [61]: frame.apply(f)
Out[61]:
b d e
min -1.696226 -2.529817 -0.644810
max 2.574698 0.488870 1.704059
逐元素的 Python 函數(shù)也可使用啸盏,如 applymap 方法
In [62]: format = lambda x: '%.2f' % x
In [63]: frame.applymap(format)
Out[63]:
b d e
Utah 0.05 -2.53 0.37
Ohio -1.70 -0.24 -0.47
Texas 2.57 -2.01 -0.64
Oregon -0.74 0.49 1.70
之所以命名為 applymap 塘安,是因為 Series 有 map 方法
In [64]: frame['e'].map(format)
Out[64]:
Utah 0.37
Ohio -0.47
Texas -0.64
Oregon 1.70
Name: e, dtype: object