2017-06-16 26 views
1
for Country in energy: 
    if energy[Country] == 'United States': 

這是遍歷熊貓中稱爲能量的DataFrame。能源所有國家都按國家字母順序列出列。 df energy如果我在如果陳述中寫出來,但如果我只是做返回,它總是會給我一個ValueError。ValueError:一個Series的真值不明確。使用a.empty,a.bool(),a.item(),a.any()或a.all()。進行字符串比較

--------------------------------------------------------------------------- 
ValueError        Traceback (most recent call last) 
<ipython-input-61-e820508b0b91> in <module>() 
    20 
    21  return energy 
---> 22 answer_one() 

<ipython-input-61-e820508b0b91> in answer_one() 
    16 
    17  for Country in energy: 
---> 18   if energy[Country] == 'United States': 
    19    return 
    20 

/opt/conda/lib/python3.5/site-packages/pandas/core/generic.py in __nonzero__(self) 
    890   raise ValueError("The truth value of a {0} is ambiguous. " 
    891       "Use a.empty, a.bool(), a.item(), a.any() or a.all()." 
--> 892       .format(self.__class__.__name__)) 
    893 
    894  __bool__ = __nonzero__ 

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all(). 
+3

不要在問題中放置數據鏈接,在問題中插入示例,沒有人希望點擊未知的外部鏈接。 – Ding

+0

你可以給出整個錯誤跟蹤 – depperm

+0

@Ding對不起,第一次發佈,該鏈接去圖像DataFrame的結構。 – veridian

回答

0

請試試:

{ 
a = energy[Country] == 'United States' 
    if a.any(): # if any one is Ture, return True' 
    if a.all(): # if the all is Ture, return True. else,return False' 
} 

也許它可以幫助你。

0

您可能想要if Country == 'United States':而不是if energy[Country] == 'United States':。後者將「美國」欄中的值與字符串「美國」進行比較(並返回一系列值)。

相關問題