# Pandas: Calculating the best seller to incorporate number of houses sold

I have been analysing seller data and trying to get insights. I have written a groupby statement to get the average price of selling for every seller

for seller,seller_df in g:

df=df.append({'Seller':seller,'AveragePrice':seller_df.Price.mean() }, ignore_index=True)
print(seller)
df.sort_values(by='AveragePrice',ascending=False,inplace=True)
plt.figure(figsize=(15,30))
sns.barplot(data=df.dropna()[:100],y='Seller',x='AveragePrice')


The bar plot is only for the top 100 sellers and shows who has the max average selling price.

I believe this method is inaccurate as I also should consider the amount of properties that he has sold in order to get the best seller. For e.g.: if A sold 20 houses with average 5 and B sold 200 with average 5, B should be the winner.So I have two questions:

1. How can I incorporate this in my code?

2. Can I optimise the code snippet?

I am a newbie and any help is appreciated. Please refer to this link for my Kaggle notebook. It does not contain the code posted here as I am doing the analysis locally but you can have a look at the data.

Dataset

• datascience.stackexchange.com/q/29908/35644. This might help you to plot groupby pd, Also there is no link in your question, kindly edit it for the us to help you further.., it seems that you want to add the count also.... Commented May 29, 2018 at 19:50
• I have no issues in plotting. What I am trying to ask is that I would consider a seller better than the other even if the average price at which they sell their houses are same if, the latter sells more houses. I want to know how to calculate this average by incorporating that count into my code here. I do not know how to do it. Commented May 29, 2018 at 23:28
• You define a new metric and use that instead of 'AveragePrice'
– oW_
Commented May 29, 2018 at 23:37
• How do I do that in Pandas easily? I can use value_counts for the frequency of occurrence and use it. But I am unclear about the next steps. Commented May 29, 2018 at 23:45
• which is better, average of 4 with 1000 sales compared to 5 average of say 50 sales? Commented May 30, 2018 at 4:55

df = seller_df.groupby(by='Seller')['Price'].sum().reset_index()