如何对单个列使用apply()函数? [英] How can I use the apply() function for a single column?

查看:55
本文介绍了如何对单个列使用apply()函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个两列的pandas数据框.我需要在不影响第二列的情况下更改第一列的值,并只更改第一列的值即可获取整个数据帧.如何在大熊猫中使用Apply?

I have a pandas data frame with two columns. I need to change the values of the first column without affecting the second one and get back the whole data frame with just first column values changed. How can I do that using apply in pandas?

推荐答案

给出一个示例数据框 df 为:

Given a sample dataframe df as:

a,b
1,2
2,3
3,4
4,5

你想要的是什么

df['a'] = df['a'].apply(lambda x: x + 1)

返回的

   a  b
0  2  2
1  3  3
2  4  4
3  5  5

这篇关于如何对单个列使用apply()函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆