
This Pandas exercise project will help Python developers to learn and practice pandas. Pandas is an open-source, BSD-licensed Python library. Pandas is a handy and useful data-structure tool for analyzing large and complex data.
Practice DataFrame, Data Selection, Group-By, Series, Sorting, Searching, statistics. Practice Data analysis using Pandas.
In this exercise, we are using Automobile Dataset for data analysis. This Dataset has different characteristics of an auto such as body-style, wheel-base, engine-type, price, mileage, horsepower, etc.
Also Read:
What included in this Pandas exercise?
- It contains 10 questions. The solution is provided for each question.
- Each question includes a specific Pandas topic you need to learn.
When you complete each question, you get more familiar with data analysis using pandas.
Exercise 1: From the given dataset print the first and last five rows
Expected Output:


Show Solution
Print first five rows
import pandas as pd
df = pd.read_csv("D:\\Python\\Articles\\pandas\\automobile-dataset\\Automobile_data.csv")
df.head(5)
Print last five rows
import pandas as pd
df = pd.read_csv("D:\\Python\\Articles\\pandas\\automobile-dataset\\Automobile_data.csv")
df.tail(5)
Exercise 2: Clean the dataset and update the CSV file
Replace all column values which contain ?
, n.a
, or NaN
.
Show Solution
df = pd.read_csv("D:\\Python\\Articles\\pandas\\automobile-dataset\\Automobile_data.csv", na_values={
'price':["?","n.a"],
'stroke':["?","n.a"],
'horsepower':["?","n.a"],
'peak-rpm':["?","n.a"],
'average-mileage':["?","n.a"]})
print (df)
df.to_csv("D:\\Python\\Articles\\pandas\\automobile-dataset\\Automobile_data.csv")
Exercise 3: Find the most expensive car company name
Print most expensive car’s company name and price.
Expected Output:

Show Solution
import pandas as pd
df = pd.read_csv("D:\\Python\\Articles\\pandas\\automobile-dataset\\Automobile_data.csv")
df = df [['company','price']][df.price==df['price'].max()]
df
Exercise 4: Print All Toyota Cars details
Expected Output:

Show Solution
import pandas as pd
df = pd.read_csv("D:\\Python\\Articles\\pandas\\automobile-dataset\\Automobile_data.csv")
car_Manufacturers = df.groupby('company')
toyotaDf = car_Manufacturers.get_group('toyota')
toyotaDf
Exercise 5: Count total cars per company
Expected Outcome:

Show Solution
import pandas as pd
df = pd.read_csv("D:\\Python\\Articles\\pandas\\automobile-dataset\\Automobile_data.csv")
df['company'].value_counts()
Exercise 6: Find each company’s Higesht price car
Expected Outcome:

Show Solution
import pandas as pd
df = pd.read_csv("D:\\Python\\Articles\\pandas\\automobile-dataset\\Automobile_data.csv")
car_Manufacturers = df.groupby('company')
priceDf = car_Manufacturers['company','price'].max()
priceDf
Exercise 7: Find the average mileage of each car making company
Expected Output:

Show Solution
import pandas as pd
df = pd.read_csv("D:\\Python\\Articles\\pandas\\automobile-dataset\\Automobile_data.csv")
car_Manufacturers = df.groupby('company')
mileageDf = car_Manufacturers['company','average-mileage'].mean()
mileageDf
Exercise 8: Sort all cars by Price column
Expected Output:

Show Solution
import pandas as pd
carsDf = pd.read_csv("D:\\Python\\Articles\\pandas\\automobile-dataset\\Automobile_data.csv")
carsDf = carsDf.sort_values(by=['price', 'horsepower'], ascending=False)
carsDf.head(5)
Exercise 9: Concatenate two data frames using the following conditions
Create two data frames using the following two dictionaries.
GermanCars = {'Company': ['Ford', 'Mercedes', 'BMV', 'Audi'], 'Price': [23845, 171995, 135925 , 71400]}
japaneseCars = {'Company': ['Toyota', 'Honda', 'Nissan', 'Mitsubishi '], 'Price': [29995, 23600, 61500 , 58900]}
Expected Output:

Show Solution
import pandas as pd
GermanCars = {'Company': ['Ford', 'Mercedes', 'BMV', 'Audi'], 'Price': [23845, 171995, 135925 , 71400]}
carsDf1 = pd.DataFrame.from_dict(GermanCars)
japaneseCars = {'Company': ['Toyota', 'Honda', 'Nissan', 'Mitsubishi '], 'Price': [29995, 23600, 61500 , 58900]}
carsDf2 = pd.DataFrame.from_dict(japaneseCars)
carsDf = pd.concat([carsDf1, carsDf2], keys=["Germany", "Japan"])
carsDf
Exercise 10: Merge two data frames using the following condition
Create two data frames using the following two Dicts, Merge two data frames, and append the second data frame as a new column to the first data frame.
Car_Price = {'Company': ['Toyota', 'Honda', 'BMV', 'Audi'], 'Price': [23845, 17995, 135925 , 71400]}
car_Horsepower = {'Company': ['Toyota', 'Honda', 'BMV', 'Audi'], 'horsepower': [141, 80, 182 , 160]}
Expected Output:

Show Solution
import pandas as pd
Car_Price = {'Company': ['Toyota', 'Honda', 'BMV', 'Audi'], 'Price': [23845, 17995, 135925 , 71400]}
carPriceDf = pd.DataFrame.from_dict(Car_Price)
car_Horsepower = {'Company': ['Toyota', 'Honda', 'BMV', 'Audi'], 'horsepower': [141, 80, 182 , 160]}
carsHorsepowerDf = pd.DataFrame.from_dict(car_Horsepower)
carsDf = pd.merge(carPriceDf, carsHorsepowerDf, on="Company")
carsDf
Exercises are good but waay too easy… please post some more exercises with increased difficulty. Thanks 🙂
Sir I want more to practice please provide more question
Nicely explained set of practice problems. Thank you.
great boosts up, thanks for the exercises,
You’re welcome, Chari Manepalli.
these exercises are very helpful thanks for your work. I will definitely look for other exercises too. I you are healthy. Lots of love.
Thanks for sharing this! It was super helpful for me to get the basics of pandas! Keep up the share <3
Hi Vishal, this is a good exercise. Thanks for providing this. However, I couldn’t execute exercise 4 with the given code.
Very excellent explanation
Thank you my friend
Super! Ultra! Great! Python Tutorial!
Thank you, Dohyun Jung.
Thank you very much! Super helpful at the first steps with pandas.
thanks alot, it really helps me out to exercise
You’re welcome, Amanjot.
for question 7:
About Question 6: Find each company’s Higesht price car
This solution code will return max value of all the columns that we include to be printed. So, for example, if I add wheel-base column, then the wheel-base value corresponding to the mercedes-benz company will be 120.9 instead of the 112 which corresponds to the row with max price 45400.
Any way to do this?
Good practice for me. Thank you.
It was helpful, thanks!
You’re welcome, Sara.
Wow.
This exercise helped me a lot in reinforcing the concepts.
Found no issues with this exercise.It is great.
Just need more such exercises to practice.
Thank you, Janesh.
Exercise 2 could be better. There’s no null values in the data to begin with.
Nice exercise please add more such exercises
Question 3.
could you provide the link to the data please, preferably in CSV format it would be a real help so that we work with the real data, otherwise great job overall thank you.
Hey Noel Danton, Please download this Automobile Data Set
This exercise is really helpful it helps in reminding and and working with all the functions at
once. i hope you will add more no of exercise in the practice series.
can you please add isnull functionality with data sets ?
for question 4:
Great content 🙂
hi where I will get tutorial on pandas library for beginners with explanation.
Hey Sonal you can read pandas documentation
I learned your pythonmysql, it was good. could you do a tutorial on pandas
Thank you for your suggestions, Jenish dev
Hi Vishal, is there any possibility we can practice online with this data. Thank you very much
Hey Manuel Juarez, unfortunately, we don’t have this feature right now. we are planning to add the interactive exercises in coming months
Very Very useful excercises sir. Please add few more questions around the same lines of pandas and datafram. You can raise the complexity level as well. please provide similar excercises on EDA(Binning, Missing Values, Outlier treatment etc.)
May God Bless you.
Hey Girish, I appreciate your kind words, thank you. And, Yes I will add more such exercises with complex problems.
Hii, this exercise is really very helpful while learning pandas.
I don’t understand solution of question 3.
Can u please explain the solution of question no. 3 ?
This exercise is very helpful in practising Pandas.
Please add some more datasets and exercises.
Thank you, Vishnu Konjeti. yes sure I will add new datasets and exercises