Using csv module to read the data in Pandas
The so-called CSV (Comma Separated Values) format is the most common import and export format for spreadsheets and databases. There were various formats of CSV until its standardization. The lack of a well-defined standard means that subtle differences often exist in the data produced and consumed by different applications. These differences can make it annoying to process CSV files from multiple sources. For that purpose, we will use Python’s
csv library to read and write tabular data in CSV format.
For link to the CSV file used in the code click here.
Code #1: We will use
csv.DictReader() function to import the data file into the Python’s enviornment.
As we can see the data is stored as a list of ordered dictionary. Let’s perform some operations on the data for better understanding.
As we can see in the output, we have 5 unique values of cylinders in our dataset.
Code #3: Now let’s find out the value of average mpg for each value of cylinders.
As we can see in the output, the program has successfully returned a list of tuples containing the average mpg for each unique cylinder type in our dataset.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course