我在分析美國的輪詢數據,具體來說,我試圖找出哪些國家是安全的,邊緣的或者緊密的('緊密度')。我有一個調查結果的時間和他們的「親密度」的數據框。我正在使用熊貓的聲明來獲得「親密」條目的摘要。從熊貓系列中選擇行是陣列的行
s=self.daily.groupby('State')['closeness'].unique()
這給我這個系列(選擇爲簡潔起見示出):
State
AK [safe]
AL [safe]
CA [safe]
CO [safe, tight, marginal]
FL [marginal, tight]
IA [safe, tight, marginal]
ID [safe]
IL [safe]
IN [tight, safe]
Name: closeness, dtype: object
的行是類型的陣列,因此,例如,s[0]
給出:
array(['safe'], dtype=object)
我試圖從這個系列中進行選擇,但是我無法正確地理解語法。例如,我想用這個語法只需選擇「安全」的國家:
ipdb> s[s == 'safe']
*** ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
這不起作用或者:
test[test == ['safe'])
這是我想要做什麼:選擇「邊際」或「緊張」的國家,選擇「安全」且僅「安全」的國家等等。有沒有人對我應該使用的語法有所瞭解,或者首先有更好的方法?
============ 這裏的數據GROUPBY前一個樣本:
ipdb> self.daily.head(3)
Date Democratic share Margin Method Other share \
0 2008-11-04 0.378894 -0.215351 Election 0.026861
1 2008-11-04 0.387404 -0.215765 Election 0.009427
2 2008-11-04 0.388647 -0.198512 Election 0.024194
Republican share State closeness winner
0 0.594245 AK safe Republican
1 0.603169 AL safe Republican
你可以在做'groupby'之前發佈樣本數據嗎? – shivsn
謝謝shivsn - 添加樣本到問題 –