Pandas groupby count只返回一列?

时间:2016-05-03 21:34:59

标签: python excel pandas dataframe

我对熊猫编程很新。我有一个文件excel,我放入一个数据框,我试图用一个count()为一个属性组成一个组,如下面的代码,然后在一个情节栏中显示这些项目的频率我' ve分组(y轴为频率,x轴为项目):

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]])

但如果我这样做,这段代码会打印下面的结果,就好像它是一个独特的列:

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

如何将两列分开?

1 个答案:

答案 0 :(得分:0)

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])

如需绘图,请参阅: Plotting categorical data with pandas and matplotlib