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

  • 相关阅读:
    家庭问题(family)
    BFS简单题记
    【例2-3】围圈报数
    【例8.3】最少步数
    【例3-5】扩展二叉树
    股票买卖
    小球(drop)
    用循环单链表实现约瑟夫环
    二叉树的3种遍历6种实现
    const与#define宏常量 , inline与#define
  • 原文地址:https://www.cnblogs.com/xinping-study/p/8134931.html
Copyright © 2011-2022 走看看