Python 如果列值为true,则将条件语句应用于其他列。级数的真值是模糊误差
我有一个根据条件语句['gap pos']返回True或False的列 根据该列是真是假,我需要另一列来遵循if-ELIF语句中的特定条件。[“填补空白”] 代码如下:Python 如果列值为true,则将条件语句应用于其他列。级数的真值是模糊误差,python,pandas,valueerror,Python,Pandas,Valueerror,我有一个根据条件语句['gap pos']返回True或False的列 根据该列是真是假,我需要另一列来遵循if-ELIF语句中的特定条件。[“填补空白”] 代码如下: mgn.loc[mgn['gap_size'] <= 0, 'gap pos'] = False mgn.loc[mgn['gap_size'] >= 0, 'gap pos'] = True if (mgn['gap pos'] == True): mgn.loc[mgn['Open2Close'] &l
mgn.loc[mgn['gap_size'] <= 0, 'gap pos'] = False
mgn.loc[mgn['gap_size'] >= 0, 'gap pos'] = True
if (mgn['gap pos'] == True):
mgn.loc[mgn['Open2Close'] <= -1* (mgn['gap_size']), 'gap filled?'] = 'Filled'
mgn.loc[mgn['Open2Close'] > -1* (mgn['gap_size']), 'gap filled?'] = 'Not filled'
elif (mgn['gap pos'] == False):
mgn.loc[mgn['Open2Close'] >= abs(mgn['gap_size']), 'gap filled?'] = 'Filled'
mgn.loc[mgn['Open2Close'] < abs(mgn['gap_size']), 'gap filled?'] = 'Not filled'
mgn.loc[mgn['gap_size']=0,“gap pos']=True
如果(mgn['gap pos']==True):
mgn.loc[mgn['Open2Close']-1*(mgn['gap_size']),'gap filled?']='Not filled'
elif(mgn['gap pos']==假):
mgn.loc[mgn['Open2Close']>=abs(mgn['gap_size']),'gap filled?']='filled'
mgn.loc[mgn['Open2Close']
我不想将.any()放在{if(mgn['gap pos']==True)}之后,因为满足了if语句,所以不会执行我的elif语句。但是如果我不放.any(),.item(),.all()等等
如果我没有把收到的任何东西放进去:
ValueError:序列的真值不明确。使用a.empty、a.bool()、a.item()、a.any()或a.all()
我需要IF/ELIF语句来迭代数据帧中的每一行 对于if和elif条件,可以将if条件带到内部。你能试试这个吗
mgn.loc[(mgn['Open2Close'] <= -1* (mgn['gap_size'])) & (mgn['gap pos'] == True), 'gap filled?'] = 'Filled'
mgn.loc[(mgn['Open2Close'] > -1* (mgn['gap_size'])) & (mgn['gap pos'] == True) , 'gap filled?'] = 'Not filled'
mgn.loc[(mgn['Open2Close'] >= abs(mgn['gap_size'])) & (mgn['gap pos'] == False), 'gap filled?'] = 'Filled'
mgn.loc[(mgn['Open2Close'] < abs(mgn['gap_size'])) & (mgn['gap pos'] == False), 'gap filled?'] = 'Not filled'
mgn.loc[(mgn['Open2Close']-1*(mgn['gap_size'])和(mgn['gap pos']==True),'gap filled?']='Not filled'
mgn.loc[(mgn['Open2Close']>=abs(mgn['gap_size'])和(mgn['gap pos']==False),'gap filled?']='filled'
mgn.loc[(mgn['Open2Close']
您是否可以尝试使用此条件而不是if和elif条件<代码>mgn.loc[(mgn['Open2Close']-1*(mgn['gap_size'])和(mgn['gap pos']==True),'gap filled?']='Not filled'对elif条件执行相同操作。用和标志将if条件带到里面,就像我做的那样。这很有效,谢谢!看起来熊猫需要&或|只要可能。谢谢你的努力和帮助。