zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    Fire
    Apple Tree
    访问艺术馆
    三角关系
    字母表
    折纸
    旅行
    单词分类

    圆桌游戏
  • 原文地址:https://www.cnblogs.com/xinping-study/p/8134931.html
Copyright © 2011-2022 走看看