• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

pynative

A Python Programming blog

Header Right

 
  • Tutorials
  • Python Exercises
  • <>Code Editor
  • Python Tricks
  • Newsletter
  • Tutorials
  • Python Exercises
  • <>Code Editor
  • Python Tricks
  • Newsletter

Python Pandas Exercise

Filed Under: Python Exercises | November 22, 2019 Leave a Comment

This Pandas exercise project will help Python developer 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.

Python Pandas Exercise

This exercise is part of a Python Exercises with Solutions

Practice DataFrame, Data  Selection,  Group-By, Series, Sorting, Searching, statistics. Practice Data analysis using Pandas. For this exercise, we are using Automobile Dataset.  This Automobile Dataset has a different characteristic of an auto such as body-style, wheel-base, engine-type, price, mileage, horsepower and many more.

Download Automobile data Set

What included in this Pandas exercise?

This exercise contains 10 questions. The solution 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.

Question 1: From given data set print first and last five rows

Expected Output:

Python Pandas printing first 5 rows
Python Pandas printing first 5 rows

 

Python Pandas printing last 5 rows
Python Pandas printing last 5 rows

Printing first five rows: –

import pandas as pd
df = pd.read_csv("D:\\Python\\Articles\\pandas\\automobile-dataset\\Automobile_data.csv")
df.head(5)

Printing last five rows: –

import pandas as pd
df = pd.read_csv("D:\\Python\\Articles\\pandas\\automobile-dataset\\Automobile_data.csv")
df.tail(5)

Question 2: Clean data and update the CSV file

Replace all column values which contain ‘?’ and n.a with NaN.

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")

Question 3: Find the most expensive car company name

Print most expensive car’s company name and price.

Expected Output:

Python Pandas printing most costly car name
Python Pandas printing most costly car name
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

Question 4: Print All Toyota Cars details

Expected Output:

Python Pandas printing all Toyota cars data
Python Pandas printing all Toyota cars data
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

Question 5: Count total cars per company

Expected Outcome:

Python Pandas count total cars per company
Python Pandas count total cars per company
import pandas as pd
df = pd.read_csv("D:\\Python\\Articles\\pandas\\automobile-dataset\\Automobile_data.csv")
df['company'].value_counts()

Question 6: Find each company’s Higesht price car

Expected Outcome:

Python Pandas printing each company's highest price car

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

Question 7: Find the average mileage of each car making company

Expected Output:

Python Pandas printing average mileage of each car making company
Python Pandas printing average mileage of each car making company
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

Question 8: Sort all cars by Price column

Expected Output:

Python Pandas sort all cars by price column
Python Pandas sort all cars by price column
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)

Question 9: Concatenate two data frames using the following conditions

Create two data frames using the following two Dicts, Concatenate those two data frames and create a key for each data frame.

GermanCars = {'Company': ['Ford', 'Mercedes', 'BMV', 'Audi'], 'Price': [23845, 171995, 135925 , 71400]}
japaneseCars = {'Company': ['Toyota', 'Honda', 'Nissan', 'Mitsubishi '], 'Price': [29995, 23600, 61500 , 58900]}

Expected Output:

Python Pandas concatenate two data frames and create key for each data frame
Python Pandas concatenate two data frames and create a key for each data frame
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

Question 10: Merge two data frames using the following condition

Create two data frames using the following two Dicts, Merge two data frames, and append 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:

Python Pandas merge two data frames and append new data frame as new column
Python Pandas merge two data frames and append new data frame as a new column
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

About Vishal

Founder of pynative.com. I am a Python developer and I love to write articles to help developers. Follow me on Twitter | Happy Pythoning!

Python Exercises
Python Tricks

Keep Learning

  • Python Basics (9)
  • Python Generate random data (12)
  • Python MySQL (12)
  • Python PostgreSQL (6)
  • Python SQLite (9)
  • Python Exercises (11)

Join Our Newsletter

I'm determined to improve your Python skills, are You? Subscribe and Get New Python Tutorials, Exercises, Tips and Tricks into your Inbox Every alternate Week.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

Python Exercises


Practice Python using our free exercises.
Exercises cover Python Basics to Data analytics and Database.
Show Exercises

Topics


  • Python Basics (9)
  • Python Generate random data (12)
  • Python MySQL (12)
  • Python PostgreSQL (6)
  • Python SQLite (9)
  • Python Exercises (11)

Join Our Newsletter


Subscribe toGet New Python Tutorials, Exercises, Tips and Tricks into your Inbox

Follow Us On

Facebook Twitter

Secondary Sidebar

Python Exercises

  • Python Basic Exercise for Beginners
  • Python String Exercise
  • Python Data Structure Exercise
  • Python Random Data Generation Exercise
  • Python NumPy Exercise
  • Python Pandas Exercise
  • Python Matplotlib Exercise
  • Python Database Exercise

  • Python Tutorials
  • Python Exercises
  • Python Tips and Tricks
  • Python Code Editor

Footer

Follow Pynative

  • Home
  • NewsLetter
  • About Us
  • Facebook
  • Twitter
  • RSS | Sitemap

Python

  • Python Tutorials
  • Python Exercises
  • Online Python Code Editor
  • Python Tricks

Join Our Newsletter

Subscribe and Get New Python Tutorials, Exercises, Tips and Tricks into your Inbox Every alternate Week.

Legal Stuff

  • Privacy Policy
  • Cookie Policy
  • Terms Of Use
  • Contact Us
  • Do Not Sell My Personal Information
DMCA.com Protection Status

Copyright © 2018-2019 · [pynative.com]

We use cookies to ensure that you have the best possible experience on our website.AcceptLearn More