pandas map, apply, applymap区别
2017-12-28 11:47 xplorerthik 阅读(2250) 评论(0) 编辑 收藏 举报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