Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何筛选数据帧并更新特定单元格的选定行_Python_Pandas_Dataframe - Fatal编程技术网

Python 如何筛选数据帧并更新特定单元格的选定行

Python 如何筛选数据帧并更新特定单元格的选定行,python,pandas,dataframe,Python,Pandas,Dataframe,当另一列上的某些条件通过时,我想更新DF中的特定列。这是我正在尝试的,但它给出了错误 train[train['Rain]'==1]['Price']=100 因此,对于所有行,当Rain column为1时,对于该行,price column应设置为100,您能否给出一个通过where和where函数使用它的示例 使用 您可以使用以下三个选项 train.loc[train['Rain'] == 1, ['Price']] = 100 或 或者使用“at”操作符 train.at[trai

当另一列上的某些条件通过时,我想更新DF中的特定列。这是我正在尝试的,但它给出了错误

train[train['Rain]'==1]['Price']=100
因此,对于所有行,当Rain column为1时,对于该行,price column应设置为100,您能否给出一个通过where和where函数使用它的示例

使用


您可以使用以下三个选项

train.loc[train['Rain'] == 1, ['Price']] = 100

或者使用“at”操作符

train.at[train['Rain'] == 1, ['Price']] = 100
希望有帮助

import numpy as np
train['Price'] = np.where(train['Rain'] == 1, 100,train['Price'])
train.at[train['Rain'] == 1, ['Price']] = 100