Menu
Coddy logo textTech

Car to Buy

Lesson 19 of 19 in Coddy's Pandas Analytics course.

When using .sort_values it returns a dataframe where column_name is converted to index:

res = df.sort_values(by='column_name', ascending=True)

An index is like an address; that's how any data point across the dataframe or series can be accessed.

When a column is converted to an index, it is no longer possible to access it the way we learned:

res["column_name"]

This is not possible.

To convert the index back to a normal column, write: .reset_index:

res = df.sort_values(by='column_name', ascending=True).reset_index()

Or make it two-stage steps:

res = df.sort_values(by='column_name', ascending=True)
res = res.reset_index()
challenge icon

Challenge

Medium

The CSV files car_raw_stats.csv and car_features.csv contain information about cars for sale.

Here is the first 5 lines of car_raw_stats.csv:

car_id,brand,price,popularity,year
1,ram,12584.93337,0.962070375,2008
2,mazda,15123.47674,0.356163012,2012
3,kia,15861.89672,0.110720597,2006
4,renault,12631.39906,0.153823182,2016

Here is the first 5 lines of car_features.csv:

car_id,sits,has_phone_charger,is_comfortable
1,4,,1
2,4,,1
3,4,1,1
4,2,1,

We need to find potential cars to buy.

  • Any missing value is considered a 0.
  • Find the mean of each brand and investigate only the top 7 brands. Filter all cars from these brands.
  • We are looking for a budget car - price smaller than 20000 but the year should be bigger than 2005.
  • The car should have 4 sits, we don't need a charger, but it needs to be comfortable. 
  • Sort the cars in ascending order by the car id.

Save the result to df

Try it yourself

# pandas as pd is already imported
df_feat = pd.read_csv("./car_features.csv")
df_stat = pd.read_csv("./car_raw_stats.csv")
# Write your code below

All lessons in Pandas Analytics