pandas map, apply, applymap区别


map只对一个序列而言的。

apply只是整个dataframe上任意一列或多列,或者一行或多行, 即可在任意轴操作。 在一列使用apply时,跟map效果一样。 多列时只能用apply。

applymap 在整个dataframe的每个元素使用一个函数。 

 

Map: It iterates over each element of a series.
df[‘column1’].map(lambda x: 10+x), this will add 10 to each element of column1.
df[‘column2’].map(lambda x: ‘AV’+x), this will concatenate “AV“ at the beginning of each element of column2 (column format is string).

Apply: As the name suggests, applies a function along any axis of the DataFrame.
df[[‘column1’,’column2’]].apply(sum), it will returns the sum of all the values of column1 and column2.

ApplyMap: This helps to apply a function to each element of dataframe.
func = lambda x: x+2
df.applymap(func), it will add 2 to each element of dataframe (all columns of dataframe must be numeric type)

参考文献 

https://chrisalbon.com/python/pandas_apply_operations_to_dataframes.html

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM