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函数参数处指定自定义函数.自定义函数同时对多列进行计算,返回计算结果即可,详见代码.
python dataframe 针对多列执行map操作的更多相关文章
- 【跟着stackoverflow学Pandas】 - Adding new column to existing DataFrame in Python pandas - Pandas 添加列
最近做一个系列博客,跟着stackoverflow学Pandas. 以 pandas作为关键词,在stackoverflow中进行搜索,随后安照 votes 数目进行排序: https://stack ...
- 解决升级Spark2.0之后,DataFrame map操作报错
当我们在使用spark1.6的时候,当我们创建SQLContext读取一个文件之后,返回DataFrame类型的变量可以直接.map操作,不会报错.但是升级之后会包一个错误,如下: 报错:No imp ...
- [Spark][Python][DataFrame][SQL]Spark对DataFrame直接执行SQL处理的例子
[Spark][Python][DataFrame][SQL]Spark对DataFrame直接执行SQL处理的例子 $cat people.json {"name":" ...
- python中的zip、lambda、map操作
python 中有几个比较酷炫的操作,比如:zip.lambda.map 一.zip操作 zip字面意思:拉链.这么来记,把几个东西扔到一个包里,拉上拉链,就算打包好了.通俗点讲,就是把第1个参数.与 ...
- 转:"为自动填充列调整大小期间不能执行此操作"解决办法 .
转载自:http://blog.csdn.net/zhxingway/article/details/5384690 今天在测试的时候,打开一个窗口突然发现出现以下错误,就算我在打开窗口那个按钮里面和 ...
- Python dataframe中如何使y列按x列进行统计?
如图:busy=0 or 1,求出busy=1时los的平均,同样对busy=0时也求出los的平均 Python dataframe中如何使y列按x列进行统计? >> python这个答 ...
- [Spark][Python]DataFrame where 操作例子
[Spark][Python]DataFrame中取出有限个记录的例子 的 继续 [15]: myDF=peopleDF.where("age>21") In [16]: m ...
- [Spark][Python]DataFrame select 操作例子II
[Spark][Python]DataFrame中取出有限个记录的 继续 In [4]: peopleDF.select("age","name") In ...
- [Spark][Python]DataFrame select 操作例子
[Spark][Python]DataFrame中取出有限个记录的例子 的 继续 In [4]: peopleDF.select("age")Out[4]: DataFrame[a ...
随机推荐
- MFC实现为窗体添加的背景图片
将一个bmp图片添加到资源中 在资源视图中更改位图资源的ID为IDB_BITMAP_BACKGROUND. 第一种方法: 在Dialog中添加一个Picture Control控件,将Picture ...
- 简单实现contentOS下开机自动启动tomcat
看过网上很多写tomcat开机自启动的例子,很多都是写了一个比较复杂的脚步.找到一个比较简单的. 首先编辑 vi /etc/rc.d/rc.local 在尾部加入 export JDK_HOME=/u ...
- Efim and Strange Grade
Efim and Strange Grade time limit per test 1 second memory limit per test 256 megabytes input standa ...
- IE6 7 父级元素的overflow:hidden 是包不住子级的relative
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- Swift 开发中Alamofire的使用
Swift 开发中Alamofire的使用 Alamofire需要解决的6个问题 1.Json解析 2.下载和上传 4.全局401错误 5.网络图片
- 转:lr_eval_string,lr_save_string 和 sprintf 的使用
lr_eval_string,lr_save_string和 sprintf 函数使用介绍 一.lr_eval_string 使用介绍1.函数的主要作用:返回脚本中的一个参数当前的值,返回值类型:ch ...
- 方便android开发网站:
可以通过输入包名或者Google Play地址消灾google市场的应用 http://apps.evozi.com/apk-downloader/ 16进制转常用的10进制 http://www.b ...
- 执行 apt-get -f install 提示错误
执行 apt-get -f install 提示错误 分类: Linux 2015-01-24 21:26 554人阅读 评论(0) 收藏 举报 1. 问题: usloft1359:~# rvm in ...
- NVIDA 提到的 深度框架库
BidMachBlocksCaffeChainerCNTKcuda-convnetcuda-convnet2Deeplearning4jkaldiKerasLasagneMarvinMatConvNe ...
- OleDB连接字符串很基础却很重要
--Oracle<add name="HISConnectionString" connectionString=" Server=127.0.0.1;Provid ...