Having a question. I am having a list of records and there is another list of records which i am comparing the first list. When i write line(inside row reading of first list:
for index, row in output_merged_po.iterrows():
stock = output_merged_stock[output_merged_stock['PN_STRIPPED']==row['PN_STRIPPED']][['Whs']]
print stock
I get result
Whs
11763 VLN
Where 11763 is output_merged_stock id number and Whs is name of whs where PN_stripped matches.
But i fail to extract data for further processing. I just want to write simple if statetement where i can ask if whs = VLN
. I wrote:
if stock[['Whs']] == 'VLN':
print stock
I got error: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
I wrote:
if stock == 'VLN':
print stock
And i got again : The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
How should i write if statement if i want to get result 'VLN'? As for example there are sometimes cases when stock output is sometimes 3 whs, where 2 of them are 'VLN' and third is 'XRS' and on that case i should see of 'if' output 2 times VLN without XRS
You're trying to compare a df, which is unnecessary here by the way, with a scalar value which is incorrect as it becomes ambiguous for testing a scalar value against because you may have 1 or more matches.
I think you want:
if all(stock['Whs']] == 'VLN'):
or if you know there is only a single row then:
if stock['Whs'].values[0] == 'VLN':
example:
In [79]:
# create some dummy data
df = pd.DataFrame({'a':np.arange(5), 'b':2})
df
Out[79]:
a b
0 0 2
1 1 2
2 2 2
3 3 2
4 4 2
try something like what you tried:
if df['a'] == 2:
print("we have 2")
which raises:
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
So we could take the hint from the error:
In [82]:
if any(df['a'] == 2):
print("we have 2")
we have 2
We can use all
with 'b' column:
In [83]:
if all(df['b'] == 2):
print("all are 2")
all are 2
If you compared a series which had a single row value then you could do this:
In [84]:
if df.iloc[2]['a'] == 2:
print("we have 2")
we have 2
but it becomes ambiguous with more than 1 row:
if df.iloc[1:3]['b'] == 2:
print("we have 2")
again raises ValueError
but the following would work:
In [87]:
if df.iloc[1:3]['b'].values[0] == 2:
print("we have 2")
we have 2