Supponiamo che io sono un dataframe di creato in questo modo:Girare Pandas dataframe di stringhe in istogramma
import pandas as pd
s1 = pd.Series(['a', 'b', 'a', 'c', 'a', 'b'])
s2 = pd.Series(['a', 'f', 'a', 'd', 'a', 'f', 'f'])
d = pd.DataFrame({'s1': s1, 's2', s2})
C'è un bel po 'di scarsità nelle stringhe nei dati reali. Vorrei creare istogrammi dell'occorrenza di stringhe che assomigliano a ciò che viene generato da d.hist() (ad esempio con sottotrami) per s1 e s2 (uno per sottotramma).
Solo facendo d.hist() dà questo errore:
/Library/Python/2.7/site-packages/pandas/tools/plotting.pyc in hist_frame(data, column, by, grid, xlabelsize, xrot, ylabelsize, yrot, ax, sharex, sharey, **kwds)
1725 ax.xaxis.set_visible(True)
1726 ax.yaxis.set_visible(True)
-> 1727 ax.hist(data[col].dropna().values, **kwds)
1728 ax.set_title(col)
1729 ax.grid(grid)
/Library/Python/2.7/site-packages/matplotlib/axes.pyc in hist(self, x, bins, range, normed, weights, cumulative, bottom, histtype, align, orientation, rwidth, log, color, label, stacked, **kwargs)
8099 # this will automatically overwrite bins,
8100 # so that each histogram uses the same bins
-> 8101 m, bins = np.histogram(x[i], bins, weights=w[i], **hist_kwargs)
8102 if mlast is None:
8103 mlast = np.zeros(len(bins)-1, m.dtype)
/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy/lib/function_base.pyc in histogram(a, bins, range, normed, weights, density)
167 else:
168 range = (a.min(), a.max())
--> 169 mn, mx = [mi+0.0 for mi in range]
170 if mn == mx:
171 mn -= 0.5
TypeError: cannot concatenate 'str' and 'float' objects
Suppongo che potrei andare manualmente attraverso ogni serie, fare un value_counts()
, poi tracciare come un grafico a barre, e creare manualmente le sottotrame. Volevo verificare se esiste un modo più semplice.
ag, bastonatemi! sì, il contatore è lo strumento per il lavoro! –
Grazie per la risposta. value_counts fa la stessa cosa, ed è una trasformazione Series -> Series (quindi non c'è bisogno di forzarla in una serie). Immagino che mi stavo chiedendo se ci fosse qualche possibilità di fare questo conteggio e tracciare per me automaticamente per questo specifico caso di stringhe, perché ce n'è una per gli interi. – amatsukawa