說我有這個數據幀「無法解釋輸入」與Seaborn錯誤
d = { 'Path' : ['abc', 'abc', 'ghi','ghi', 'jkl','jkl'],
'Detail' : ['foo', 'bar', 'bar','foo','foo','foo'],
'Program': ['prog1','prog1','prog1','prog2','prog3','prog3'],
'Value' : [30, 20, 10, 40, 40, 50],
'Field' : [50, 70, 10, 20, 30, 30] }
df = DataFrame(d)
df.set_index(['Path', 'Detail'], inplace=True)
df
Field Program Value
Path Detail
abc foo 50 prog1 30
bar 70 prog1 20
ghi bar 10 prog1 10
foo 20 prog2 40
jkl foo 30 prog3 40
foo 30 prog3 50
我可以聚集沒有問題(如果有一個更好的方式來做到這一點,順便說一下,我想知道!)
df_count = df.groupby('Program').count().sort(['Value'], ascending=False)[['Value']]
df_count
Program Value
prog1 3
prog3 2
prog2 1
df_mean = df.groupby('Program').mean().sort(['Value'], ascending=False)[['Value']]
df_mean
Program Value
prog3 45
prog2 40
prog1 20
我可以從熊貓繪製它沒有問題... ...
df_mean.plot(kind='bar')
可是爲什麼我得到這個錯誤時我在seaborn嘗試它?
sns.factorplot('Program',data=df_mean)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-26-23c2921627ec> in <module>()
----> 1 sns.factorplot('Program',data=df_mean)
C:\Anaconda3\lib\site-packages\seaborn\categorical.py in factorplot(x, y, hue, data, row, col, col_wrap, estimator, ci, n_boot, units, order, hue_order, row_order, col_order, kind, size, aspect, orient, color, palette, legend, legend_out, sharex, sharey, margin_titles, facet_kws, **kwargs)
2673 # facets to ensure representation of all data in the final plot
2674 p = _CategoricalPlotter()
-> 2675 p.establish_variables(x_, y_, hue, data, orient, order, hue_order)
2676 order = p.group_names
2677 hue_order = p.hue_names
C:\Anaconda3\lib\site-packages\seaborn\categorical.py in establish_variables(self, x, y, hue, data, orient, order, hue_order, units)
143 if isinstance(input, string_types):
144 err = "Could not interperet input '{}'".format(input)
--> 145 raise ValueError(err)
146
147 # Figure out the plotting orientation
ValueError: Could not interperet input 'Program'
非常感謝回覆。起初我認爲這是一個索引問題。但根據[文檔](http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.groupby.html),默認情況下'as index'參數爲True,因此組標籤(即'Program')已經是索引。 'df_mean.index' 'Index(['prog3','prog2','prog1'],dtype ='object',name ='Program')' 我嘗試了第二種方法,並收到相同的錯誤以及。 – marshallbanana
我不確定我們互相瞭解。無論如何,你對'as_index'參數做了一個好點,我正在更新答案。希望現在更清楚。 – lrnzcig
對不起 - 我只是意識到我們對這個指數說同樣的話。我認爲factorplot默認情況下能夠使用x軸的索引。所以我很困惑,你的第二個解決方案返回相同的錯誤 – marshallbanana