python - Unable to plot dataframe using seaborn barplot -
i have been able use pandas groupby create new dataframe i'm getting error when create barplot. groupby command:
invyr = invoices.groupby(['finyear']).sum()[['amount']]
which creates new dataframe looks correct me.
running:
sns.barplot(x='finyear', y='amount', data=invyr)
i error:
valueerror: not interperet input 'finyear'
it appears issue related index, being finyear unfortunately have not been able solve issue when using reindex.
import pandas pd import seaborn 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 getting error when created invyr grouping invoices, finyear column becomes index , no longer column. there few solutions:
1) 1 solution specify source data directly. need specify correct datasource chart. if not specify data parameter, seaborn not know dataframe/series has columns 'finyear' or 'amount' these text values. must specify, example, y=invyr.amount specify both dataframe/series , column you'd graph. trick here directly accessing index of dataframe.
sns.barplot(x=invyr.index, y=invyr.amount) 2) alternatively, can specify data source , directly refer columns. note grouped data frame had index reset column again becomes available.
sns.barplot(x='finyear', y='amount', data=invyr.reset_index()) 3) third solution specify as_index=false when perform groupby, making column available in grouped dataframe.
invyr = invoices.groupby('finyear', as_index=false).amount.sum() sns.barplot(x='finyear', y='amount', data=invyr) all solutions above produce same plot below.

Comments
Post a Comment