pandas 无法使用 seaborn barplot 绘制数据框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35168016/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 00:36:32  来源:igfitidea点击:

Unable to plot dataframe using seaborn barplot

pythonpandasseaborn

提问by sams

I have been able to use pandas groupbyto create a new DataFramebut I'm getting an error when I create a barplot. The groupby command:

我已经能够使用 Pandasgroupby创建一个新的,DataFrame但是当我创建一个barplot. groupby 命令:

invYr = invoices.groupby(['FinYear']).sum()[['Amount']]

invYr = invoices.groupby(['FinYear']).sum()[['Amount']]

Which creates a new DataFramethat looks correct to me.

这创造了一个DataFrame对我来说看起来正确的新事物。

New DataFrameinvYr

新的DataFrameinvYr

Running:

跑步:

sns.barplot(x='FinYear', y='Amount', data=invYr)

sns.barplot(x='FinYear', y='Amount', data=invYr)

I get the error:

我收到错误:

ValueError: Could not interperet input 'FinYear'

ValueError: Could not interperet input 'FinYear'

It appears that the issue is related to the index, being FinYear but unfortunately I have not been able to solve the issue even when using reindex.

问题似乎与索引有关,即 FinYear 但不幸的是,即使使用reindex.

回答by Alexander

import pandas as pd
import seaborn as sns

invoices = pd.DataFrame({'FinYear': [2015, 2015, 2014], 'Amount': [10, 10, 15]})
invYr = invoices.groupby(['FinYear']).sum()[['Amount']]

>>> invYr
         Amount
FinYear        
2014         15
2015         20

The reason that you are getting the error is that when you created invYrby grouping invoices, the FinYearcolumn becomes the index and is no longer a column. There are a few solutions:

您收到错误的原因是,当您invYr通过 grouping创建时invoices,该FinYear列成为索引而不再是列。有几种解决方案:

1) One solution is to specify the source data directly. You need to specify the correct datasource for the chart. If you do not specify a dataparameter, Seaborn does not know which dataframe/series has the columns 'FinYear' or 'Amount' as these are just text values. You must specify, for example, y=invYr.Amountto specify both the dataframe/series and the column you'd like to graph. The trick here is directly accessing the index of the dataframe.

1) 一种解决方案是直接指定源数据。您需要为图表指定正确的数据源。如果您不指定data参数,Seaborn 将不知道哪个数据框/系列具有“FinYear”或“Amount”列,因为这些只是文本值。例如,您必须指定y=invYr.Amount数据框/系列和您想要绘制的列。这里的技巧是直接访问数据帧的索引。

sns.barplot(x=invYr.index, y=invYr.Amount)

2) Alternatively, you can specify the data source and then directly refer to its columns. Note that the grouped data frame had its index reset so that the column again becomes available.

2) 或者,您可以指定数据源,然后直接引用其列。请注意,分组数据框的索引已重置,以便该列再次可用。

sns.barplot(x='FinYear', y='Amount', data=invYr.reset_index())

3) A third solution is to specify as_index=Falsewhen you perform the groupby, making the column available in the grouped dataframe.

3) 第三种解决方案是指定as_index=False何时执行groupby,使该列在分组数据框中可用。

invYr = invoices.groupby('FinYear', as_index=False).Amount.sum()
sns.barplot(x='FinYear', y='Amount', data=invYr)

All solutions above produce the same plot below.

上面的所有解决方案都会产生下面相同的图。

enter image description here

在此处输入图片说明