Python 在具有多个元素条件的dataframe中设置值

Python 在具有多个元素条件的dataframe中设置值,python,pandas,location,element,Python,Pandas,Location,Element,我有一个数据帧。我想将具有特定参数值的行中小于x的所有值设置为新值 我尝试了这个,但是我的数据帧没有发生任何变化 df_data = {'A': [4, 4, 5, 5], 'B': [4, 4, 4, 5], 'C': [4, 5, 5, 5], 'Bool': [True, True, False, False]} test_df = pd.DataFrame(df_data, colum

我有一个数据帧。我想将具有特定参数值的行中小于x的所有值设置为新值

我尝试了这个,但是我的数据帧没有发生任何变化

   df_data = {'A': [4, 4, 5, 5],
               'B': [4, 4, 4, 5],
               'C': [4, 5, 5, 5],
               'Bool': [True, True, False, False]}
    test_df = pd.DataFrame(df_data, columns=['A', 'B', 'C', 'Bool'])
    test_df[test_df.iloc[:, :-1] < 5][test_df['Bool'] == True] = 99
    print(test_df)
df_data={'A':[4,4,5,5],
“B”:[4,4,4,5],
‘C’:[4,5,5,5],
'Bool':[True,True,False,False]}
test_df=pd.DataFrame(df_数据,列=['A','B','C','Bool'])
test_df[test_df.iloc[:,:-1]<5][test_df['Bool']==True]=99
打印(测试测向)

我希望test_df中的某些元素的值为99。

m=(test_-df.iloc[:,:-1]<5)。值和test_-df['Bool']。值[:,无]
打印(m)
[[True]
[真假]
[假]
[假假假]]
test_-df.iloc[:,:-1]=test_-df.iloc[:,:-1]。掩码(m,99)
打印(测试测向)
一本书
0 99 99对
199995对
2 5 4 5错误
3.5.5错误
这项工作:

m = (test_df.iloc[:, :-1] < 5) & (pd.DataFrame(pd.np.tile((test_df['Bool'].values[:, None] == True), (1, 3)))).values
test_df.iloc[:, :-1] = test_df.iloc[:, :-1].mask(m, 99)
m=(test_-df.iloc[:,:-1]<5)和(pd.DataFrame(pd.np.tile((test_-df['Bool'])值[:,None]==True),(1,3))。值
test_-df.iloc[:,:-1]=test_-df.iloc[:,:-1]。掩码(m,99)

您编写的代码不起作用。您完全正确,因为我忘记了
loc
,对不起-
test_df.loc[(test_df.iloc[:,:-1]<5)&test_df['Bool','A']=99
我收到错误消息:ValueError:操作数不能与形状(16,)(4,)一起广播,我看到了。从示例数据中导出的输出是什么?dfu data={'A':[99,99,5,5],'B':[99,99,99,5],'C':[4,5,5],'Bool':[True,True,False,False]}我在创建m:ValueError:无法强制到数据帧时收到错误消息,形状必须是(4,3):给定的(4L,1L)m=(test_df.iloc[:,:-1]<5)和(pd.DataFrame(pd.np.tile((test_-df['Bool'].values[:,None]==True),(1,3)))。values test_-df.iloc[:,:-1]=test_-df.iloc[:,:-1]=test_-df.iloc[:,:,:-1]。mask(m,99)@muzzex你能测试
m=(test_-df.iloc[,:,:,:,-1]。值<5)和test_-df['Bool']!
m = (test_df.iloc[:, :-1] < 5) & (pd.DataFrame(pd.np.tile((test_df['Bool'].values[:, None] == True), (1, 3)))).values
test_df.iloc[:, :-1] = test_df.iloc[:, :-1].mask(m, 99)