在 Pandas DataFrame 上更新行子集的列值的有效方法? [英] Efficient way to update column value for subset of rows on Pandas DataFrame?

查看:57
本文介绍了在 Pandas DataFrame 上更新行子集的列值的有效方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当使用 Pandas 更新特定行子集的列值时,最好的方法是什么?

When using Pandas to update the value of a column for specif subset of rows, what is the best way to do it?

简单的例子:

import pandas as pd

df = pd.DataFrame({'name' : pd.Series(['Alex', 'John', 'Christopher', 'Dwayne']),
                   'value' : pd.Series([1., 2., 3., 4.])})

目标:根据名称长度和值列本身的初始值更新value列.

Objective: update the value column based on names length and the initial value of the value column itself.

以下行实现了目标:

df.value[df.name.str.len() == 4 ] = df.value[df.name.str.len() == 4] * 1000

然而,这一行在 LHS 和 RHS 中对整个数据帧进行了两次过滤.我认为这不是最有效的方式.它并没有就地"完成.

However, this line filters the whole data frame two times, both in LHS and RHS. I assume is not the most efficient way. And it does not do it 'in place'.

基本上我正在寻找相当于 R data.table ':=' operator:

Basically I'm looking for the pandas equivalent to R data.table ':=' operator:

df[nchar(name) == 4, value := value*1000]

对于其他类型的操作,例如:

And for other kind of operations such:

df[nchar(name) == 4, value := paste0("short_", as.character(value))]

环境:Python 3.6 Pandas 0.22

提前致谢.

推荐答案

这可能是您所需要的:

 df.loc[df.name.str.len() == 4, 'value'] *= 1000

 df.loc[df.name.str.len() == 4, 'value'] = 'short_' + df['value'].astype(str)

这篇关于在 Pandas DataFrame 上更新行子集的列值的有效方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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