map ,apply ,applymap的区别

apply 让函数作用于列或者行,applymap可以作用于每一个元素,map主要作用于series上的元素

In[4] frame
out[4] a b c
0 -0.019455 -0.503332 -0.375681
1 -1.677207 0.598891 0.679787
2 0.364526 0.322883 -2.313652
3 1.169806 0.143207 -0.426937
4 -0.327589 0.083359 -0.497328

In[5] frame.apply(lambda x:x.mean())
out[5] a -0.097984
b 0.129001
c -0.586762
dtype: float64
In[6] frame.map(lambda x:x.mean())
out[6] ---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
in ()
----> 1 frame.map(lambda x:x.mean())

~/anaconda3/lib/python3.7/site-packages/pandas/core/generic.py in getattr(self, name)
4374 if self._info_axis._can_hold_identifiers_and_holds_name(name):
4375 return self[name]
-> 4376 return object.getattribute(self, name)
4377
4378 def setattr(self, name, value):

AttributeError: ‘DataFrame’ object has no attribute ‘map’
In[7] frame.applymap(lambda x:x3)
out[7] a b c
0 -0.058366 -1.509997 -1.127042
1 -5.031620 1.796672 2.039360
2 1.093579 0.968649 -6.940955
3 3.509417 0.429620 -1.280811
4 -0.982768 0.250078 -1.491985
In[8] frame[‘a’].map(lambda x:x
3)
out[8] 0 -0.038911
1 -3.354413
2 0.729053
3 2.339611
4 -0.655179
Name: a, dtype: float64

猜你喜欢

转载自blog.csdn.net/weixin_42595856/article/details/83928850