python - Print value from a list (The truth value of a DataFrame is ambiguous error) -
having question. having list of records , there list of records comparing first list. when write line(inside row reading of first list:
index, row in output_merged_po.iterrows(): stock = output_merged_stock[output_merged_stock['pn_stripped']==row['pn_stripped']][['whs']] print stock
i result
whs 11763 vln
where 11763 output_merged_stock id number , whs name of whs pn_stripped matches.
but fail extract data further processing. want write simple if statetement can ask if whs = vln
. wrote:
if stock[['whs']] == 'vln': print stock
i got error: the truth value of dataframe ambiguous. use a.empty, a.bool(), a.item(), a.any() or a.all().
i wrote:
if stock == 'vln': print stock
and got again : the truth value of dataframe ambiguous. use a.empty, a.bool(), a.item(), a.any() or a.all().
how should write if statement if want result 'vln'? example there cases when stock output 3 whs, 2 of them 'vln' , third 'xrs' , on case should see of 'if' output 2 times vln without xrs
you're trying compare df, unnecessary here way, scalar value incorrect becomes ambiguous testing scalar value against because may have 1 or more matches.
i think want:
if all(stock['whs']] == 'vln'):
or if know there single row then:
if stock['whs'].values[0] == 'vln':
example:
in [79]: # create dummy data df = pd.dataframe({'a':np.arange(5), 'b':2}) df out[79]: b 0 0 2 1 1 2 2 2 2 3 3 2 4 4 2
try tried:
if df['a'] == 2: print("we have 2")
which raises:
valueerror: truth value of series ambiguous. use a.empty, a.bool(), a.item(), a.any() or a.all().
so take hint error:
in [82]: if any(df['a'] == 2): print("we have 2") have 2
we can use all
'b' column:
in [83]: if all(df['b'] == 2): print("all 2") 2
if compared series had single row value this:
in [84]: if df.iloc[2]['a'] == 2: print("we have 2") have 2
but becomes ambiguous more 1 row:
if df.iloc[1:3]['b'] == 2: print("we have 2")
again raises valueerror
following work:
in [87]: if df.iloc[1:3]['b'].values[0] == 2: print("we have 2") have 2
Comments
Post a Comment