Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Finding average value in pandas

I have this pandas Dataframe:

name        price
Hyundai     $25000
Hyundai     $35000
Peugeot     $43250
BMW         $80000
Hyundai     $23000
Peugeot     $46000
BMW         $90000
Peugeot     $30500

and I want to find the name that has biggest price average using pandas library.

Any help would be appreciated.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Remove $ with converting values to numeric, here integers, aggregate mean and last get name by maximal value by Series.idxmax:

out = df['price'].str.strip('$').astype(int).groupby(df['name']).mean().idxmax()
print (out)
BMW

Details:

print (df['price'].str.strip('$').astype(int))
0    25000
1    35000
2    43250
3    80000
4    23000
5    46000
6    90000
7    30500
Name: price, dtype: int32

print (df['price'].str.strip('$').astype(int).groupby(df['name']).mean())
name
BMW        85000.000000
Hyundai    27666.666667
Peugeot    39916.666667
Name: price, dtype: float64
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading