Python 熊猫群比命令后与seaborn的情节

Python 熊猫群比命令后与seaborn的情节,python,pandas,seaborn,pandas-groupby,Python,Pandas,Seaborn,Pandas Groupby,我用熊猫将一个列表分组,并试图用seaborn绘制下表: B A bar 3 foo 5 代码sns.countplot(x='A',data=df)不工作(ValueError:无法解释输入'A') 我可以只使用df.plot(kind='bar'),但我想知道是否可以使用seaborn进行绘图。试试: sns.countplot(x='A', data=df.reset_index()) A列似乎是索引。尝试: sns.countplot(

我用熊猫将一个列表分组,并试图用seaborn绘制下表:

     B  
A           
bar  3  
foo  5  
代码
sns.countplot(x='A',data=df)
不工作
(ValueError:无法解释输入'A')

我可以只使用
df.plot(kind='bar')
,但我想知道是否可以使用seaborn进行绘图。

试试:

sns.countplot(x='A', data=df.reset_index())
A列似乎是索引。

尝试:

sns.countplot(x='A', data=df.reset_index())

似乎列A是一个索引。

在本例中,我认为您可能缺少一个重置索引,因此可以使用该索引

 sns.countplot(x='A', data=df.reset_index())
此外,请检查色调参数以进行分组,这可能会使groupby变得不必要

import seaborn as sns
df = pd.DataFrame( [['A', 'B', 'A'], [1,1,1], [4,5,4]], index=['g', 'x', 'y']).T
sns.countplot(data=df, x='y', hue='g')

在这种情况下,我认为您可能缺少一个重置索引,因此您可以使用该索引

 sns.countplot(x='A', data=df.reset_index())
此外,请检查色调参数以进行分组,这可能会使groupby变得不必要

import seaborn as sns
df = pd.DataFrame( [['A', 'B', 'A'], [1,1,1], [4,5,4]], index=['g', 'x', 'y']).T
sns.countplot(data=df, x='y', hue='g')

谢谢@Darren Brien。.reset_index()没有按照我的要求绘制,但是您的示例解决了我的问题。我尝试过,但我没有足够的声誉来这样做。谢谢@Darren Brien。.reset_index()没有按照我的要求绘制,但是您的示例解决了我的问题。我尝试过,但我没有足够的声誉来这样做。