Difference between map, applymap and apply methods in Pandas

前端 未结 10 2012
刺人心
刺人心 2020-11-22 03:00

Can you tell me when to use these vectorization methods with basic examples?

I see that map is a Series method whereas the rest are

10条回答
  •  佛祖请我去吃肉
    2020-11-22 03:04

    My understanding:

    From the function point of view:

    If the function has variables that need to compare within a column/ row, use apply.

    e.g.: lambda x: x.max()-x.mean().

    If the function is to be applied to each element:

    1> If a column/row is located, use apply

    2> If apply to entire dataframe, use applymap

    majority = lambda x : x > 17
    df2['legal_drinker'] = df2['age'].apply(majority)
    
    def times10(x):
      if type(x) is int:
        x *= 10 
      return x
    df2.applymap(times10)
    

提交回复
热议问题