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.

new dataframe invyr

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.

enter image description here


Comments

Popular posts from this blog

Delphi XE2 Indy10 udp client-server interchange using SendBuffer-ReceiveBuffer -

Qt ActiveX WMI QAxBase::dynamicCallHelper: ItemIndex(int): No such property in -

Enable autocomplete or intellisense in Atom editor for PHP -