For any and all questions relating to Challenge 11 post away!

This question was difficult and took me longer than I expected.

There are a few recipes for doing this question. (If there are more, feel free to reply and let me know)

One way

1. List the min and max price of each neighborhood. The function suggested in the tutorial might be helpful here.
2. Use pandas data frame indexing (could be by column name or index) to subtract the smaller row from the bigger row.
3. Sort the values, then only show the few biggest.

Second Way

1. Instead of listing the min and max of each neighborhood, find a function that can find calculate the range (ie. do step 1 AND step 2 of the previous recipe at once). It doesnât have to be a panadas functionâŚit could be from another data science library that starts with an âNâ
2. Sort the values, then show the few biggest.

I hope this was helpful without giving away too much

Update: See @YGW 's post further down for a third way!

8 Likes

A Third Way:
(Directly Find Row Instead of Sorting)

1. Make a new âdifferenceâ column in the DataFrame. This can be done using one of the ways you describe. (For how to add a column, see here, and/or see below code example.)

Edit1: This link gives a more complete explanation of how to add a difference column.

1. Directly find row with max value using `loc` and `idxmax`, instead of sorting. (See here for how.)

2. Display result using `print()` method or `head()` method (after using a filter).

``````# HOW TO ADD A NEW COLUMN

df["NAME_FOR_NEW_COLUMN"] = LIST_OR_SERIES_FOR_NEW_COLUMN

# Replace NAME_FOR_NEW_COLUMN with any name for your new column
# Replace LIST_OR_SERIES_FOR_NEW_COLUMN with
# a list or series that is the new column you want to add
# The new column can be one that is derived from the existing columns
``````
3 Likes

Which Is Better:
Directly Finding Max (Or Min) Through Search Function OR
Sorting First?

How To Search For Item Directly
You can use the idxmax and loc functions. See here!

Which Is Better
Not sure! But maybe this is how we can figure it out:

One way to decide between the two would be to decide which is more efficient. From what I gather (from quora and stackoverflow), asking the DataFrame to sort its values by some column before we search through it is:

• less efficient if we have very few items that need to be sorted or if we are going to search through the DataFrame only a few times, and
• more efficient if we have a long list of items AND we are going to search through it many times

Thoughts?

Anyone have any thoughts on which is better? Or on if it even matters which method we use (sort or direct search)?

2 Likes

Great explanations guys!! Thanks for always helping.

5 Likes

Is there an aggregate function under groupby that can providee range for different list of catrgorical values in a column?

1 Like

Good solution loc+idxmax
I was tired to use only the sort_values and head(1).
It will be my first option from now on!

1 Like

Hi all, I will be moderating the forum for the next 2 hours. Please let me know if you face any barriers in attempting to complete todays challenge. Also, please remember to not share any answers on this forum.

Denver

Iâm stumped on this one. Iâve used the .agg function as described to list off the max and min prices in columns. The things Iâve tried are ânot callableâ. How do I âcallâ the columns to subtract them?

I had such a busy day and was only able to do this 8PM my time. I used your first solution and got it in 10mins

I had to make 3 variables (PriceMax, PriceMin, and Sorted). PriceMax I agg the max price. PriceMin I agg the min price. Sorted I max minus min. Then I sorted the Sorted variable

This one is pretty frustrating (Iâm a python noob). Iâm using the grouping by neighbourhood using price and max(), then sorting to make it ascending, like we did in yesterdayâs challenge. So I can get the max and min values, but I donât know how to do the dataframe indexing to subtract one from the other.

Eg, I have this code:

``````groupedMax = df.groupby('neighborhood')[['price']].max()
maxPrice = groupedMax.sort_values('price',ascending=False)
``````

But Iâm not sure what to do next

I ran into the same trouble. A little digging led me to this. If you take their example of:

df.groupby([âneighborhoodâ]).agg({âpriceâ : [âmeanâ,âmaxâ]})

you can access the columns like this:
df.groupby([âneighborhoodâ]).agg({âpriceâ : [âmeanâ,âmaxâ]})[âpriceâ][âmeanâ] #for the mean column
df.groupby([âneighborhoodâ]).agg({âpriceâ : [âmeanâ,âmaxâ]})[âpriceâ][âmaxâ] #for the max column

A possible syntax: df[ânew_columnâ] = df[âold_column_1â] - df[âold_column_2â] if thatâs what youâre referring to.

I finally got it. Thank you, @Android451 and @JD2022!

Thanks @Denverdias !

I found that syntax, but Iâm not sure how to make the âoldâ columns. if I do:

`df["maxPrice"] = df.groupby(['neighborhood']).agg({'price' : ['max']})`

I just get NaN in that new column.

df[âmaxPriceâ] = df.groupby([âneighborhoodâ], as_index=False)[âpriceâ].max() would be a better way of phrasing that.

Any way to download the data and play with it after the challenges?

Check out this post! It explains: