1. 函数应用
df = pd.DataFrame(np.arange(12).reshape(4,3), columns=list(‘bde’), index=[1,2,3,4])
np.sign(df1)
a b c d
1 0 1 1 1
2 1 1 1 1
3 1 1 1 1
15 用lambda表达式处理行或列
f = lambda x: x.max()-x.min()
df
Out[357]:
b d e
1 0 1 2
2 3 4 5
3 6 7 8
4 9 10 11
df.apply(f, axis =1)
Out[355]:
1 2
2 2
3 2
4 2
dtype: int64
df.apply(f, axis =0)
Out[356]:
b 9
d 9
e 9
dtype: int64
16 用自定义函数处理元素
def f(x):
return ‘%.2f’ % x
df.applymap(f)
Out[359]:
b d e
1 0.00 1.00 2.00
2 3.00 4.00 5.00
3 6.00 7.00 8.00
4 9.00 10.00 11.00