2016-08-05 142 views
1

我在分析美國的輪詢數據,具體來說,我試圖找出哪些國家是安全的,邊緣的或者緊密的('緊密度')。我有一個調查結果的時間和他們的「親密度」的數據框。我正在使用熊貓的聲明來獲得「親密」條目的摘要。從熊貓系列中選擇行是陣列的行

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 
+0

你可以在做'groupby'之前發佈樣本數據嗎? – shivsn

+0

謝謝shivsn - 添加樣本到問題 –

回答

1

假設你有一個系列列表的數據幀,說:

df = pd.DataFrame({'a': [['safe'], ['safe', 'tight'], []]}) 

然後,看看哪些是完全安全的,你可以使用:

In [7]: df.a.apply(lambda x: x == ['safe']) 
Out[7]: 
0  True 
1 False 
2 False 
Name: a, dtype: bool 

要找到那些其含你可以使用:

In [9]: df.a.apply(lambda x: 'safe' in x) 
Out[9]: 
0  True 
1  True 
2 False 
Name: a, dtype: bool 

等等。由OP給出

0

數據幀樣本:

In[66]:df 
Out[66]: 
     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 
3 2008-11-04   0.384547 -0.194545 Election  0.024194 
4 2008-11-04   0.345330 -0.194512 Election  0.024459 

    Republican share State closeness  winner 
0   0.594245 AK  safe Republican 
1   0.603169 AL  safe Republican 
2   0.454545 CA  tight Democratic 
3   0.453450 CO marginal Democratic 
4   0.454545 FL  tight Republic 

然後用grupby:

In[67]:s=df.groupby('State')['closeness'].unique() 

In[68]:s 
Out[68]: 
State 
AK  [safe] 
AL  [safe] 
CA  [tight] 
CO [marginal] 
FL  [tight] 

然後使用np.where

In[69]:s.ix[np.where(s=='safe')] 
Out[69]: 
State 
AK [safe] 
AL [safe] 
Name: closeness, dtype: object 
+0

這給了我一個空系列... –

+0

@ AlbertoGarcia-Raboso不,如果你按照op的方式去做,他會得到一系列值的系列,我會更新回答。 – shivsn

+0

讓我們繼續討論[聊天](https://chat.stackoverflow.com/rooms/120309/selecting-rows-from-pandas-series-where-rows-are-arrays)。 –

0

我想用.unique()是構建系列s不是攻擊這個問題的最好方法。改爲使用pd.crosstab

import pandas as pd 

daily = pd.DataFrame({'State': ['AK', 'AL', 'CA', 'CO', 'CO', 'CO', 'FL', 
           'FL', 'IA', 'IA', 'IA', 'ID', 'IL', 'IN', 'IN'], 
         'closeness': ['safe', 'safe', 'safe', 'safe', 'tight', 
            'marginal', 'marginal', 'tight', 'safe', 
            'tight', 'marginal', 'safe', 'safe', 
            'tight', 'safe']}) 
ct = pd.crosstab(daily['State'], daily['closeness']) 
print(ct) 

輸出:

closeness marginal safe tight 
State       
AK    0  1  0 
AL    0  1  0 
CA    0  1  0 
CO    1  1  1 
FL    1  0  1 
IA    1  1  1 
ID    0  1  0 
IL    0  1  0 
IN    0  1  1 

其中一方面,這ct包含完全相同的信息,您s;另一方面,它使你按照你想要的方式選擇狀態變得微不足道。你提出的兩個例子:

# states that are 'marginal' or 'tight' 
print(ct.loc[(ct['marginal'] > 0) | (ct['tight'] > 0)] 
     .index.values) 
# => ['CO', 'FL', 'IA', 'IN'] 

# States that are 'safe' and only 'safe' 
print(ct.loc[(ct['safe'] > 0) & (ct['marginal'] == 0) & (ct['tight'] == 0)] 
     .index.values) 
# => ['AK', 'AL', 'CA', 'ID', 'IL'] 

或者,使用或許更可讀.query()

# states that are 'marginal' or 'tight' 
print(ct.query('marginal > 0 | tight > 0').index.values) 
# => ['CO', 'FL', 'IA', 'IN'] 

# States that are 'safe' and only 'safe' 
print(ct.query('safe > 0 & marginal == 0 & tight == 0') 
     .index.values) 
# => ['AK', 'AL', 'CA', 'ID', 'IL'] 

然而,如果你堅持要用你的s,這裏是你如何構建從它ct

ct = s.str.join(' ').str.get_dummies(sep=' ')