2016-10-12 4 views
2

Ich habe DatenrahmenPandas: mit groupby wenn Werte in den Spalten sind Wörterbücher

category dictionary 
moto {'motocycle':10, 'buy":8, 'motocompetition':7} 
shopping {'buy':200, 'order':20, 'sale':30} 
IT {'iphone':214, 'phone':1053, 'computer':809} 
shopping {'zara':23, 'sale':18, 'sell':20} 
IT {'lenovo':200, 'iphone':300, 'mac':200} 

I groupby Kategorie und als Ergebnis concatenate Wörterbücher benötigen, und wählen Sie 3 Tasten mit den größten Werten. Und als nächstes bekomme ich einen Dataframe, wo ich in der Spalte category eine eindeutige Kategorie habe, und in der Spalte data habe ich eine Liste mit Schlüsseln.

Ich weiß, dass ich Counter verwenden kann, um dicts zu verketten, aber ich weiß nicht, wie das zu Kategorien. Wunsch Ausgang

category data 
moto ['motocycle', 'buy', 'motocompetition'] 
shopping ['buy', 'sale', 'zara'] 
IT ['phone', 'computer', 'iphone'] 
+0

können Sie ein funktionierendes Beispiel? was hast du bisher gesehen? – JMat

+0

Wenn ich nur dicts habe, ohne Dataframe, kann ich das mit 'a = {1: 2, 2: 5, 6: 9, u'cat ': 2} b = {1: 4, 4: 2, 6: 1, u'dog ': 11, u'cat': 8} c = {5: 2, 7: 1, u'dog ': 19} a = Zähler (a) b = Zähler (b) c = Zähler (c) d = a + b + c Ergebnis = dict (d.most_common (3)) list = result.keys() '@JMat –

Antwort

3

Sie groupby mit benutzerdefinierter Funktion mit nlargest und Index.tolist verwenden können:

df = pd.DataFrame({ 
'category':['moto','shopping','IT','shopping','IT'], 
'dictionary': 
[{'motocycle':10, 'buy':8, 'motocompetition':7}, 
{'buy':200, 'order':20, 'sale':30}, 
{'iphone':214, 'phone':1053, 'computer':809}, 
{'zara':23, 'sale':18, 'sell':20}, 
{'lenovo':200, 'iphone':300, 'mac':200}]}) 

print (df) 
    category           dictionary 
0  moto {'motocycle': 10, 'buy': 8, 'motocompetition': 7} 
1 shopping    {'sale': 30, 'buy': 200, 'order': 20} 
2  IT {'phone': 1053, 'computer': 809, 'iphone': 214} 
3 shopping    {'sell': 20, 'zara': 23, 'sale': 18} 
4  IT   {'lenovo': 200, 'mac': 200, 'iphone': 300} 


import collections 
import functools 
import operator 

def f(x): 
    #some possible solution for sum values of dict 
    #http://stackoverflow.com/a/3491086/2901002 
    return pd.Series(functools.reduce(operator.add, map(collections.Counter, x))) 
      .nlargest(3).index.tolist() 

print (df.groupby('category')['dictionary'].apply(f).reset_index()) 
    category       dictionary 
0  IT   [phone, computer, iphone] 
1  moto [motocycle, buy, motocompetition] 
2 shopping     [buy, sale, zara] 
+0

ich Ihnen' gmail' senden eine E-Mail, bitte prüfen Sie. – jezrael

1
df = pd.DataFrame(dict(category=['moto', 'shopping', 'IT', 'shopping', 'IT'], 
         dictionary=[ 
          dict(motorcycle=10, buy=8, motocompetition=7), 
          dict(buy=200, order=20, sale=30), 
          dict(iphone=214, phone=1053, computer=809), 
          dict(zara=23, sale=18, sell=20), 
          dict(lenovo=200, iphone=300, mac=200), 
         ])) 

def top3(x): 
    return x.dropna().sort_values().tail(3)[::-1].index.tolist() 

df.dictionary.apply(pd.Series).groupby(df.category).sum().apply(top3, axis=1) 

category 
IT     [phone, computer, iphone] 
moto  [motorcycle, buy, motocompetition] 
shopping      [buy, sale, zara] 
dtype: object 
Verwandte Themen