Home > front end >  WHAT IS THE PROPER WAY OF CREATING A FUNCTION WITH GROUPBY FUNCTION
WHAT IS THE PROPER WAY OF CREATING A FUNCTION WITH GROUPBY FUNCTION

Time:10-23

def find_corr (data,x,y):
corr=data.groupby(by=['x','y']).size()
corr=corr.reset_index().rename(columns={0:'count'}).sort_values('count', ascending=False)[:10]
plot=sns.barplot(data=corr,'count',y=y,hue=x,orient= 'h')
plt.title(f'Correlation between {y} and {x}'.title(), fontsize = 14, weight = "bold")
plt.xlabel(x.title(), fontsize = 10, weight="bold")
plt.ylabel(y.title(), fontsize = 10, weight="bold")

This function when used brings a KeyError: 'x'

CodePudding user response:

The error message leads me to suspect that "x" is not a column of data. Try data.groupby(by=[x, y]).size() if you pass the column names as x and y to your function.

CodePudding user response:

Since you are using a dataframe, you should pass the column names as string. Also, the x argument for the plot seems to be missing.

The plot line should be something like this:

plot=sns.barplot(data=corr, x='count', y='y', hue='x' ,orient= 'h')

  • Related