map ,apply ,applymap, transform的区别

本文介绍了Pandas库中apply、applymap及map函数的区别与应用场景。通过实例展示了如何使用这些函数处理DataFrame,包括按列或行应用函数、对每个元素进行操作,以及在Series上应用函数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值