0% found this document useful (0 votes)
2K views7 pages

Python Dataframe Assignment No 1 - Answerkey

This document outlines 10 questions for a Python DataFrame assignment involving an automobile dataset. The questions involve printing the first and last rows of the DataFrame, cleaning missing data, finding the most expensive car company, filtering for Toyota cars, counting cars by company, finding the highest priced car by company, calculating average mileage by company, sorting by price, concatenating DataFrames from dictionaries, and merging DataFrames on the 'Company' column.

Uploaded by

Anni Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2K views7 pages

Python Dataframe Assignment No 1 - Answerkey

This document outlines 10 questions for a Python DataFrame assignment involving an automobile dataset. The questions involve printing the first and last rows of the DataFrame, cleaning missing data, finding the most expensive car company, filtering for Toyota cars, counting cars by company, finding the highest priced car by company, calculating average mileage by company, sorting by price, concatenating DataFrames from dictionaries, and merging DataFrames on the 'Company' column.

Uploaded by

Anni Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

PYTHON DATAFRAME

ASSIGNMENT NO 1
For this assignment, download the 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.
Question 1: From given data set print first and last five rows
import pandas as pd
df = pd.read_csv("E:\\Automobile_data.csv")
print(df.head(5))

import pandas as pd
df = pd.read_csv("E:\\Automobile_data.csv")
print(df.tail(5))

Question 2: Clean data and update the CSV file


Replace all column values which contain ‘?’ and n.a with NaN.
import pandas as pd
df = pd.read_csv("E:\\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("E:\\Automobile_data.csv")

Question 3: Find the most expensive car company name


Print most expensive car’s company name and price.
import pandas as pd
df = pd.read_csv("E:\\Automobile_data.csv")
df = df [['company','price']][df.price==df['price'].max()]
print(df)

Question 4: Print All Toyota Cars details


import pandas as pd
df = pd.read_csv("E:\\Automobile_data.csv")
car_Manufacturers = df.groupby('company')
toyotaDf = car_Manufacturers.get_group('toyota')
print(toyotaDf)
Question 5: Count total cars per company
import pandas as pd
df = pd.read_csv("E:\\Automobile_data.csv")
print(df['company'].value_counts())

Question 6: Find each company’s Highest price car


import pandas as pd
df = pd.read_csv("E:\\Automobile_data.csv")
car_Manufacturers = df.groupby('company')
priceDf = car_Manufacturers['company','price'].max()
print(priceDf)

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


import pandas as pd
df = pd.read_csv("E:\\Automobile_data.csv")
car_Manufacturers = df.groupby('company')
mileageDf = car_Manufacturers['company','average-mileage'].mean()
print(mileageDf)
Question 8: Sort all cars by Price column
import pandas as pd
carsDf = pd.read_csv("E:\\Automobile_data.csv")
carsDf = carsDf.sort_values(by=['price'])
print(carsDf)
Question 9: Concatenate two data frames using following conditions
Create two data frames using following two Dicts, Concatenate those two data frame 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]}
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"])
print(carsDf)

Question 10: Merge two data frames using following condition


Create two data frames using following two Dicts, Merge two data frames, and append
second data frame as a new column to 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]}
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")
print(carsDf)

You might also like