简体   繁体   English

在条件上设置pandas数据帧的值

[英]Set value of pandas data frame on conditional

I can't find a similar question for this query. 我无法为此查询找到类似的问题。 However, I have a pandas dataframe where I want to use two of the columns to make conditional and if its true, replace the values in one of these columns. 但是,我有一个pandas数据框,我想使用两个列来生成条件,如果是真的,则替换其中一列中的值。

For example. 例如。 One of my columns is the 'itemname' and the other is the 'value'. 我的一个列是'itemname',另一个是'value'。 the 'itemname' may be repeated many times. 'itemname'可能会重复多次。 I want to check for each 'itemname', if all other items with the same name have value 0, then replace these 'value' with 100. 我想检查每个'itemname',如果所有其他具有相同名称的项目的值为0,则将这些'value'替换为100。

I know this should be simple, however I cannot get my head around it. 我知道这应该很简单,但是我无法理解它。

Just to make it clearer, here 只是为了让它更清晰,在这里

    itemname value
0      a       0
1      b       100
2      c       0
3      a       0
3      b       75
3      c       90

I would like my statement to change this data frame to 我希望我的声明能够将此数据框更改为

    itemname value
0      a       100
1      b       100
2      c       0
3      a       100
3      b       75
3      c       90

Hope that makes sense. 希望有道理。 I check if someone else has asked something similar and couldnt find something in this case. 我检查是否有人问了类似的东西,在这种情况下无法找到一些东西。

You can use GroupBy + transform to create a mask. 您可以使用GroupBy + transform来创建蒙版。 Then assign via pd.DataFrame.loc and Boolean indexing: 然后通过pd.DataFrame.loc和布尔索引进行分配:

mask = df.groupby('itemname')['value'].transform(lambda x: x.eq(0).all())
df.loc[mask.astype(bool), 'value'] = 100

print(df)

  itemname  value
0        a    100
1        b    100
2        c      0
3        a    100
3        b     75
3        c     90

Using transform with any : 使用any transform

df.loc[~df.groupby('itemname').value.transform('any'), 'value'] = 100

Using numpy.where : 使用numpy.where

s = ~df.groupby('itemname').value.transform('any')
df.assign(value=np.where(s, 100, df.value))

Using addition and multiplication: 使用加法和乘法:

s = ~df.groupby('itemname').value.transform('any')
df.assign(value=df.value + (100 * s))

Both produce the correct output, however, np.where and the final solution don't modify the DataFrame in place: 两者都产生正确的输出,但是, np.where和最终的解决方案不会修改DataFrame:

  itemname  value
0        a    100
1        b    100
2        c      0
3        a    100
3        b     75
3        c     90

Explanation 说明

~df.groupby('itemname').value.transform('any')

0     True
1    False
2    False
3     True
3    False
3    False
Name: value, dtype: bool

Since 0 is a falsey value, we can use any , and negate the result, to find groups where all values are equal to 0 . 由于0是假值,我们可以使用any ,并取消结果,以查找所有值都等于0

If all your values are positive or 0 如果您的所有值都是正数或0

Could use transform with sum and check if 0: 可以使用sum和sum检查是否为0:

m = (df.groupby('itemname').transform('sum') == 0)['value']
df.loc[m, 'value'] = 100

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM