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.
CodePudding user response:
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