pandas 从列表中打印值(DataFrame 的真值是不明确的错误)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30182594/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Print value from a list (The truth value of a DataFrame is ambiguous error)
提问by orangutangas
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 语句,我可以在那里询问 if 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().
我有错误: 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().
我又得到了: 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
如果我想得到结果“VLN”,我应该如何写 if 语句?例如,有时存货输出有时为 3 whs,其中 2 个是“VLN”,第三个是“XRS”,在这种情况下,我应该看到“if”输出 2 次 VLN 没有 XRS
采纳答案by EdChum
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 与一个不正确的标量值进行比较,这在此处是不必要的,因为它在测试标量值时变得不明确,因为您可能有 1 个或多个匹配项。
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:系列的真值不明确。使用 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 allwith 'b' column:
我们可以使用all'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 ValueErrorbut the following would work:
再次引发,ValueError但以下将起作用:
In [87]:
if df.iloc[1:3]['b'].values[0] == 2:
print("we have 2")
?
we have 2
回答by orangutangas
EdChum,
Thank you it works, but not in a manner that i would like to. For the FORfunction 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 _ 行。
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?
如何解决这个问题并且只看到一条需要看到的线?

