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

问题描述

我有一个包含两列的 Pandas 数据框.我需要在不影响第二列的情况下更改第一列的值,并在仅更改第一列值的情况下取回整个数据框.我如何在 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() 函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 17:58
查看更多