In: Computer Science
Solve using PYTHON PROGRAMMING
Write a script that reads a file “cars.csv”, into a pandas structure and then print
a. the first 3 rows and the last 3 of the dataset
b. the 3 cars with the lowest average-mileage
c. the 3 cars with the highest average-mileage.
Solve using PYTHON PROGRAMMING
import pandas as pd
cars = pd.read_csv("cars.csv")
# head returns n rows from top
print("\nFirst 3 Rows")
print(cars.head(3))
# tail returns n rows from last
print("\nLast 3 Rows")
print(cars.tail(3))
# sort the dataframe by mpg
cars = cars.sort_values(by = ['mpg'])
# returns the top 3 rows
print("\n3 cars with the Lowest average-mileage")
print(cars.head(3))
# returns the last 3 rows
print("\n3 cars with the Highest average-mileage")
print(cars.tail(3))
cars.csv
Code :
Output :