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

python dataframe 针对多列执行map操作的更多相关文章

  1. 【跟着stackoverflow学Pandas】 - Adding new column to existing DataFrame in Python pandas - Pandas 添加列

    最近做一个系列博客,跟着stackoverflow学Pandas. 以 pandas作为关键词,在stackoverflow中进行搜索,随后安照 votes 数目进行排序: https://stack ...

  2. 解决升级Spark2.0之后,DataFrame map操作报错

    当我们在使用spark1.6的时候,当我们创建SQLContext读取一个文件之后,返回DataFrame类型的变量可以直接.map操作,不会报错.但是升级之后会包一个错误,如下: 报错:No imp ...

  3. [Spark][Python][DataFrame][SQL]Spark对DataFrame直接执行SQL处理的例子

    [Spark][Python][DataFrame][SQL]Spark对DataFrame直接执行SQL处理的例子 $cat people.json {"name":" ...

  4. python中的zip、lambda、map操作

    python 中有几个比较酷炫的操作,比如:zip.lambda.map 一.zip操作 zip字面意思:拉链.这么来记,把几个东西扔到一个包里,拉上拉链,就算打包好了.通俗点讲,就是把第1个参数.与 ...

  5. 转:"为自动填充列调整大小期间不能执行此操作"解决办法 .

    转载自:http://blog.csdn.net/zhxingway/article/details/5384690 今天在测试的时候,打开一个窗口突然发现出现以下错误,就算我在打开窗口那个按钮里面和 ...

  6. Python dataframe中如何使y列按x列进行统计?

    如图:busy=0 or 1,求出busy=1时los的平均,同样对busy=0时也求出los的平均 Python dataframe中如何使y列按x列进行统计? >> python这个答 ...

  7. [Spark][Python]DataFrame where 操作例子

    [Spark][Python]DataFrame中取出有限个记录的例子 的 继续 [15]: myDF=peopleDF.where("age>21") In [16]: m ...

  8. [Spark][Python]DataFrame select 操作例子II

    [Spark][Python]DataFrame中取出有限个记录的   继续 In [4]: peopleDF.select("age","name") In ...

  9. [Spark][Python]DataFrame select 操作例子

    [Spark][Python]DataFrame中取出有限个记录的例子 的 继续 In [4]: peopleDF.select("age")Out[4]: DataFrame[a ...

随机推荐

  1. USER-AGENT是什么

    USER-AGENT是什么? USER-AGENT:记录请求所来自的浏览器. User-Agent分析网站 http://www.useragentstring.com/ 通过解析User-Agent ...

  2. 业务零影响!如何在Online环境中巧用MySQL传统复制技术【转】

    业务零影响!如何在Online环境中巧用MySQL传统复制技术 这篇文章我并不会介绍如何部署一个MySQL复制环境或keepalived+双主环境,因为此类安装搭建的文章已经很多,大家也很熟悉.在这篇 ...

  3. JSON缺包导致的错误

    Json-lib 需要的 jar 包 commons-beanutils-1.8.3.jar commons-collections-3.2.1.jar commons-lang-2.6.jar co ...

  4. CharSequence的getText()与String的getString()(转)

    CharSequence的getText()与String的getString()『Android系列七』 曾经在学习中碰见两种获取常量的方式: CharSequence chrs = getText ...

  5. Android---->FrameLayout布局方式

    main.xml布局代码分析-->实现一个渐变图案 <?xml version="1.0" encoding="utf-8"?> <Fr ...

  6. LoadRunner 录制cas 登陆脚本

    关于CAS 的概念,见链接:http://www.360doc.com/content/15/0204/17/21706453_446251626.shtml 需要增加4个关联函数 //Correla ...

  7. Mysql用户相关操作

    MySQL 默认有个root用户,但是这个用户权限太大,一般只在管理数据库时候才用.如果在项目中要连接 MySQL 数据库,则建议新建一个权限较小的用户来连接. 在 MySQL 命令行模式下输入如下命 ...

  8. SIFT

    简介 SIFT(scale invariant feature transform)——尺度不变特征转换,用来检测和描述局部特征,运用范围包括object recognition(目标检测), rob ...

  9. web容器启动顺序

    web容器启动顺序: 第一:context-param 第二:Listerer 第三:Filter 第四:servlet

  10. PAT (Advanced Level) 1022. Digital Library (30)

    简单模拟题. 写的时候注意一些小优化,小心TLE. #include<iostream> #include<cstring> #include<cmath> #in ...