[英]Pandas increasing column values conditional to another column
What's wrong with this approach这种方法有什么问题
df['max'] = np.where((df[df['category'] == 'basket']['width'] == 0), df['max']+10, df['max'])
so essentially I would like to update df['max']
based on the condition where df['category']
equals basket and the width of those equals 0
.所以基本上我想根据df['category']
等于篮子且宽度等于0
的条件更新df['max']
。 If this is the case I would like to increase all df['max']
values by 10
if not I would just like to keep it the same.如果是这种情况,我想将所有df['max']
值增加10
,否则我只想保持不变。
This returns the following error这将返回以下错误
ValueError: operands could not be broadcast together with shapes (17,) (55,) (55,)
Input输入
max category width
0 181 basket 0
1 745 basket 10
desired output所需 output
max category width
0 191 basket 0
1 745 basket 10
With given input this worked on my side.有了给定的输入,这对我有用。
df["max"] = np.where((df["category"]=="basket")&(df["width"]==0), df["max"]+10, df["max"])
print(df)
max category width
0 191 basket 0
1 745 basket 10
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.