修改 pandas 数据框中的行的子集 [英] Modifying a subset of rows in a pandas dataframe

查看:54
本文介绍了修改 pandas 数据框中的行的子集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个带有A和B两列的pandas DataFrame.我想修改此DataFrame(或创建一个副本),以便只要A为0,B始终为NaN.我将如何实现?

Assume I have a pandas DataFrame with two columns, A and B. I'd like to modify this DataFrame (or create a copy) so that B is always NaN whenever A is 0. How would I achieve that?

我尝试了以下

df['A'==0]['B'] = np.nan

df['A'==0]['B'].values.fill(np.nan)

没有成功.

推荐答案

使用 .loc 用于基于标签的索引编制:

Use .loc for label based indexing:

df.loc[df.A==0, 'B'] = np.nan

df.A==0表达式创建一个布尔系列以对行进行索引,'B'选择列.您还可以使用它来转换列的子集,例如:

The df.A==0 expression creates a boolean series that indexes the rows, 'B' selects the column. You can also use this to transform a subset of a column, e.g.:

df.loc[df.A==0, 'B'] = df.loc[df.A==0, 'B'] / 2

我对pandas内部没有足够的了解,无法确切知道为什么这样做,但是基本的问题是有时索引到DataFrame中会返回结果的副本,有时会返回原始对象的视图.根据文档此处,此行为取决于底层的numpy行为.我发现在一个操作(而不是[one] [two])中访问所有内容更可能用于设置.

I don't know enough about pandas internals to know exactly why that works, but the basic issue is that sometimes indexing into a DataFrame returns a copy of the result, and sometimes it returns a view on the original object. According to documentation here, this behavior depends on the underlying numpy behavior. I've found that accessing everything in one operation (rather than [one][two]) is more likely to work for setting.

这篇关于修改 pandas 数据框中的行的子集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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