numpy函数应用与映射
from pandas import Index
from pandas import Series,DataFrame
import numpy as np
import pandas as pd
#numpy函数在Series/DataFrame的应用
frame=DataFrame(np.arange(9).reshape(3,3),
columns= ['A','B','C'],
index= ['a','b','c'])
print(frame)
'''
A B C
a 0 1 2
b 3 4 5
c 6 7 8
'''
print(np.square(frame))
'''
A B C
a 0 1 4
b 9 16 25
c 36 49 64
'''
series=frame.A
print(series)
'''
a 0
b 3
c 6
Name: A, dtype: int32
'''
print(np.square(series))
'''
a 0
b 9
c 36
Name: A, dtype: int32
'''
#lambda(匿名函数)以及应用
print(frame)
'''
A B C
a 0 1 2
b 3 4 5
c 6 7 8
'''
print(frame.max())
'''
A 6
B 7
C 8
dtype: int32
'''
#按列
f= lambda x: x.max() - x.min()
print(frame.apply(f))
'''
A 6
B 6
C 6
dtype: int64
'''
# print(frame.apply(f), axis = 1) # ??作用到每一列
def f(x):
return Series([x.min(),x.max()],index=['min','max'])
print(frame.apply(f))
'''
A B C
min 0 1 2
max 6 7 8
'''
#applymap和map:作用到每一个元素
_format= lambda x: '%.2f'% x
print(frame.applymap(_format)) # 针对DataFrame
'''
A B C
a 0.00 1.00 2.00
b 3.00 4.00 5.00
c 6.00 7.00 8.00
'''
print(frame['A'].map(_format)) # 针对Series
'''
a 0.00
b 3.00
c 6.00
Name: A, dtype: object
'''