Seba92
Seba92

Reputation: 455

Pandas groupby count returns only a column?

I'm quite new to pandas programming. I have a file excel that I put into a dataframe and I was trying to do a group by with a count() for an attribute like in the code below and afterwards to show in a plotbar the frequency of these items I've grouped (y axis the frequency, x axis the item) :

red_whine=pd.read_csv('winequality-red.csv',header=1,sep=';',names=['fixed_acidity','volatile_acidity',...])
frequency=red_whine.groupby('quality')['quality'].count()
pdf=pd.DataFrame(frequency)
print(pdf[pdf.columns[0]])

but if I do this, this code will print me the result below like if it was a unique column:

quality
3     10
4     53
5    680
6    638
7    199
8     18

How can I keep the two columns separated?

Upvotes: 0

Views: 1453

Answers (1)

Alexander
Alexander

Reputation: 109626

import urllib2  # By recollection, Python 3 uses import urllib 

target_url = "http://archive.ics.uci.edu/ml/machine-learning-databases/wine-quality/winequality-red.csv"
wine = pd.read_csv(urllib2.urlopen(target_url), sep=';')

vc = wine.quality.value_counts()
>>> vc
5    681
6    638
7    199
4     53
8     18
3     10
Name: quality, dtype: int64

>>> vc.index
Int64Index([5, 6, 7, 4, 8, 3], dtype='int64')

>>> vc.values
array([681, 638, 199,  53,  18,  10])

For plotting, please refer to this: Plotting categorical data with pandas and matplotlib

Upvotes: 2

Related Questions