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. Ubuntu Linux系统下的SVN客户端工具PySVN

    在Windows下面一直在用TortoiseSVN做为SVN客户端工具,但它居然没提供Linux版本,无视Linux用户的存在.它视我如空 气,偶视它如废土.开始探索尝试其他跨平台的SVN客户端,最后 ...

  2. C++设计模式-单件

    理解 1.       Singleton模式用来取代全局静态变量.C++通过静态成员变量来实现类实例全局唯一性. 2.       instance()方法是单件提供的唯一调用入口.   要点 1. ...

  3. android脚步---跟随手指动的小球

    这次的学习主要是在包里面除自动生成的mainactivity.java之外,再新建一个class, DrawView,在mainactivity里面调用DrawView. 在DrawView里面主要是 ...

  4. css背景图片、隐藏、指针、垂直居中、去除下划线、缩进、列表类型

    <html><head lang="en"> <meta charset="UTF-8"> <title>< ...

  5. The Triangle 经典DP

    题意:数塔问题 思路:1:递归.2:递推.3:记忆化搜索.<刘汝佳,第九章> #include<iostream> #include<cstdio> #includ ...

  6. View如何设置16进制颜色值

    View.setBackgroundColor(Color.parseColor("#F3733F"));

  7. Chapter 1 First Sight——7

    Eventually we made it to Charlie's. 最终我们到了查理斯的家. He still lived in the small,two-bedroom house that ...

  8. word 书签排序算法

    直接上代码 /// <summary> /// 通过计算插入引文的位置格式化合适的引文序号 /// </summary> /// <returns></ret ...

  9. PAT 天梯赛 L2-004 这是二叉搜索树吗?

    递归判断+建树 题目链接:https://www.patest.cn/contests/gplt/L2-004 题解 二叉搜索树的特点就是其根节点的值是位于左右子树之间的,即大于左子树的所有值,但是小 ...

  10. 我的云服务之WWW

    云服务器系统是:[root@ip-172-31-27-132 system]# cat /etc/redhat-release Red Hat Enterprise Linux Server rele ...