zoukankan      html  css  js  c++  java
  • python dataframe 针对多列执行map操作

    Suppose I have a df which has columns of 'ID', 'col_1', 'col_2'. And I define a function :

     f = lambda x, y : my_function_expression.

    Now I want to apply the f to df's two columns 'col_1', 'col_2' to element-wise calculate a new column 'col_3' , somewhat like :  

    df['col_3'] = df[['col_1','col_2']].apply(f)

    How to do ?

    译文:怎么同时对列 col_1 和 col_2 执行map操作,生成新的一列?

    答:

    Here's an example using apply on the dataframe, which I am calling with axis = 1.

    Note the difference is that instead of trying to pass two values to the function f, rewrite the function to accept a pandas Series object, and then index the Series to get the values needed.

    In [49]: df
    Out[49]: 
              0         1
    0  1.000000  0.000000
    1 -0.494375  0.570994
    2  1.000000  0.000000
    3  1.876360 -0.229738
    4  1.000000  0.000000
    
    In [50]: def f(x):    
       ....:  return x[0] + x[1]  
       ....:  
    
    In [51]: df.apply(f, axis=1) #passes a Series object, row-wise
    Out[51]: 
    0    1.000000
    1    0.076619
    2    1.000000
    3    1.646622
    4    1.000000

    Depending on your use case, it is sometimes helpful to create a pandas group object, and then use apply on the group.

    译文:利用apply函数,在apply函数参数处指定自定义函数.自定义函数同时对多列进行计算,返回计算结果即可,详见代码.

    来源:stackoverflow

  • 相关阅读:
    linux | 一次网卡故障处理
    KVM | centos 安装 window 虚拟机
    DNS | named.run文件很大的处理方法
    01
    802.11X用户身份验证
    802.11有线等效加密WEP
    802.11成帧细节
    802.11 MAC基础
    【转载】我为什么放弃了 Linux 内核学习?
    Linux网络编程(一):一个简单的socket程序
  • 原文地址:https://www.cnblogs.com/zhoudayang/p/5564219.html
Copyright © 2011-2022 走看看