map (), apply () and applymap () distinguishable from app

to sum up:

1.apply () is an operation to make DataFrame rows or columns in the function role.
2.applymap () is an operation on each element let DataFrame function role.
3.map () is an operation of each element in the Series so that the function role.

apply (), map the difference () and applymap () is that different application scenarios of
https://blog.csdn.net/GR346305172/article/details/100174898

1.apply()

apply () function will be applied to a DataFrame rows or columns, as shown below

import pandas as pd
import numpy as np
frame = pd.DataFrame(np.random.randn(4, 3),columns=list('bde'),index=['Utah', 'Ohio', 'Texas', 'Oregon'])
frame
b	d	e
Utah	-0.774163	0.224804	-0.715506
Ohio	-0.678502	-1.864374	-0.652795
Texas	1.282988	-0.801617	-0.339106
Oregon	-1.077619	0.415863	0.454939
f = lambda x: x.max() - x.min()
frame.apply(f)
b    2.360607
d    2.280237
e    1.170445
dtype: float64

2.applymap()

applymap () DataFrame acting on each of the elements, as shown below


import pandas as pd
import numpy as np
frame = pd.DataFrame(np.random.randn(4, 3),columns=list('bde'),index=['Utah', 'Ohio', 'Texas', 'Oregon'])import pandas as pd
import numpy as np
#小数点后的2代表2位小数
format = lambda x: '%.2f' % x
frame.applymap(format)
​
b	d	e
Utah	-0.10	-0.97	0.08
Ohio	-1.33	1.58	1.43
Texas	-0.62	0.29	-0.54
Oregon	-1.80	-1.08	-1.13

3.map ()
Map () is a function of a Series, DataFrame structure unusable map (). map () function is applied to the Series sucked in each element, as follows


import pandas as pd
import numpy as np
frame = pd.DataFrame(np.random.randn(4, 3),columns=list('bde'),index=['Utah', 'Ohio', 'Texas', 'Oregon'])
format = lambda x: '%.2f' % x
frame['e'].map(format)
Utah       0.92
Ohio      -0.74
Texas      1.29
Oregon    -0.45
Name: e, dtype: object

Published 273 original articles · won praise 1 · views 4678

Guess you like

Origin blog.csdn.net/wj1298250240/article/details/104011792