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