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.
其中11763是output_merged_stock id编号,Whs是PN_stripped匹配的whs的名称。
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 statetement我可以问whs = VLN。我写:
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().
我收到错误:DataFrame的真值是不明确的。使用a.empty,a.bool(),a.item(),a.any()或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().
我再次得到:DataFrame的真值是模棱两可的。使用a.empty,a.bool(),a.item(),a.any()或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
如果我想获得结果'VLN',我该怎么写if语句?例如,有时库存输出有时为3 whs,其中2个为'VLN',第3个为'XRS',在这种情况下我应该看到'if'输出2次VLN而没有XRS
2 个解决方案
#1
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.
你试图比较一个df,顺便说一下,这个标量值是不正确的,因为它对于测试标量值变得不明确,因为你可能有一个或多个匹配。
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().
ValueError:Series的真值是不明确的。使用a.empty,a.bool(),a.item(),a.any()或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:
我们可以使用'b'列的所有内容:
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:
但是超过1行变得模棱两可:
if df.iloc[1:3]['b'] == 2:
print("we have 2")
again raises ValueError
but the following would work:
再次引发ValueError,但以下方法可行:
In [87]:
if df.iloc[1:3]['b'].values[0] == 2:
print("we have 2")
we have 2
#2
EdChum, Thank you it works, but not in a manner that i would like to. For the FOR
function i get these results then PN matches:
EdChum,谢谢你的工作,但不是我想要的方式。对于FOR函数,我得到这些结果然后PN匹配:
Whs
14883 _
15607 VKO
So if i put
所以如果我放
if stock.iloc[0:3]['Whs'].values[0] == '_':
print stock
I get result as above:
我得到的结果如上:
Whs
14883 _
15607 VKO
Where i need only see 14883 _ line.
我只需要看到14883 _ line。
However, if i write second Whs name to look for (VKO) :
但是,如果我写第二个Whs名称来寻找(VKO):
if stock.iloc[0:3]['Whs'].values[0] == 'VKO':
print stock
I get empty line, no info is printed, while i want to see
我得到空行,没有打印信息,而我想看
Whs
15607 WKO
To be printed. Same results i get with another yours function:
要打印。我得到另一个你的功能相同的结果:
if any(stock['Whs'] == '_'):
print stock
How it is possible to solve that problem and see only a line which is needed to see?
如何解决这个问题,只看到需要看到的一条线?
#1
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.
你试图比较一个df,顺便说一下,这个标量值是不正确的,因为它对于测试标量值变得不明确,因为你可能有一个或多个匹配。
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().
ValueError:Series的真值是不明确的。使用a.empty,a.bool(),a.item(),a.any()或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:
我们可以使用'b'列的所有内容:
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:
但是超过1行变得模棱两可:
if df.iloc[1:3]['b'] == 2:
print("we have 2")
again raises ValueError
but the following would work:
再次引发ValueError,但以下方法可行:
In [87]:
if df.iloc[1:3]['b'].values[0] == 2:
print("we have 2")
we have 2
#2
EdChum, Thank you it works, but not in a manner that i would like to. For the FOR
function i get these results then PN matches:
EdChum,谢谢你的工作,但不是我想要的方式。对于FOR函数,我得到这些结果然后PN匹配:
Whs
14883 _
15607 VKO
So if i put
所以如果我放
if stock.iloc[0:3]['Whs'].values[0] == '_':
print stock
I get result as above:
我得到的结果如上:
Whs
14883 _
15607 VKO
Where i need only see 14883 _ line.
我只需要看到14883 _ line。
However, if i write second Whs name to look for (VKO) :
但是,如果我写第二个Whs名称来寻找(VKO):
if stock.iloc[0:3]['Whs'].values[0] == 'VKO':
print stock
I get empty line, no info is printed, while i want to see
我得到空行,没有打印信息,而我想看
Whs
15607 WKO
To be printed. Same results i get with another yours function:
要打印。我得到另一个你的功能相同的结果:
if any(stock['Whs'] == '_'):
print stock
How it is possible to solve that problem and see only a line which is needed to see?
如何解决这个问题,只看到需要看到的一条线?