仅根据dataframe中的列名设置值[duplicate]

问题描述 投票:2回答:2

这个问题在这里已有答案:

    max_speed  shield  new
1   1          2       3
2   4          5       6

我得到了这个数据帧,我想根据一些列名分配一个值。像这样df.loc [df ['max_speed'] == 1,df ['shield'] == 2,new] = 10然后我会得到新的数据帧:

    max_speed  shield  new
 1  1          2       10
 2  4          5       6

谁知道怎么做?

python pandas
2个回答
2
投票

你很近。

df.loc[(df.max_speed == 1) & (df.shield == 2), "new"] = 10

如需进一步阅读,请参阅here


2
投票

如果您想在不符合条件的情况下更改值,请选择qazxsw boi:

where
© www.soinside.com 2019 - 2024. All rights reserved.