-2

Is there simpler way to write the following code snippet in python?

The code is attempting to go through a base list of fruits which appear more than once with different prices and understand the mean prices on a per fruit basis. The results of this is then assigned to a dictionary.

The dataframe is consisting of the fruit name and prices of these fruit. This is called "fruits_list".

chosen_fruits = [ "orange", "apple", "kiwi" ]

fruit_total_data = {}

for item in chosen_fruits:
    population = fruits_list[fruits_list["fruit"] == item]
    pop_list = fruits_list["fruit"].value_counts()
    pop_list_price_sum = fruits_list["price"].sum() 
    pop_list_count = pop_list[0]
    mean = pop_list_price_sum / pop_list
    mean_take = mean[0]
    fruit_total_data[item] = mean_take

This seems to work but feels like there could be a more succinct way to code for this. Particularly with respect to the way the mean values are being calculated for the chosen fruits.

  • 2
    Can you also please share the sample dataset and the output dictionary ? It will help process much better ! – Abhi Oct 16 '22 at 14:12
  • `df.groupby('fruits')['prices'].mean().to_dict()`. Pandas was made to deal with this kind of problems, so try to [research](https://duckduckgo.com/?q=pandas+mean+by+group) whether there's a pandas function already doing what you want instead of reinventing it. – Ignatius Reilly Oct 16 '22 at 14:50
  • Please check [How to make pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples). Note to reduce the dataset to the necessary minimum. – MagnusO_O Oct 16 '22 at 16:24

1 Answers1

-1

Consider using Pandas Groupby and aggregation.