Change language

Using the CSV Module to Read Data in Pandas

| | |

For a link to the CSV file used in the code, click here .

Code # 1: We will use csv.DictReader () to import the data file into csv.DictReader () Python.

# csv module import

import csv

 
# Now let’s read the file named & # 39; auto-mpg.csv & # 39;
# After reading how to convert a dictionary
# this is in the Python list

with open ( ’ auto-mpg.csv’ ) as csvfile:

mpg_data = list (csv.DictReader (csvfile))

 
# Let’s render the data
# We only print the first three elements

print (mpg_data [: 3 ])

Exit :

As you can see, the data is stored as a list of ordered dictionaries. Let’s do some data manipulation for a better understanding.

Code # 2:

# Let’s find everything keys in the dictionary

print (mpg_data [ 0 ]. Keys)

 
# Now we would like to know the number
# unique values ​​of cylinders in the car in our dataset
# Create a set containing the value of the cylinders

unique_cyl = set (data [ ’cylinders’ ] for data in mpg_data)

 
# Print the values ​​

print (unique_cyl)

Output:


As we can see in the output, there are 5 unique cylinder values ​​in our dataset.

Code # 3: Now let’s figure out the average mpg for each cylinder value.

# Let’s create an empty list to store the values ​​
Average mpg for each cylinder

 avg_mpg = []

 
# c - current cylinder size

for c in unique_cyl:

  # for storing mpg

mpgbycyl = 0

# to store the total of cylinders

No. in each categories

cylcount = 0

 

  # iterate over all data in mpg

  for x in mpg_data:

# Check if the current value matches c

  if x [ ’ cylinders’ ] = = c:

# Add mpg values ​​for c

mpgbycyl + = float (x [ ’mpg’ ])

# increase cylinder counter

cylcount + = 1

 

  # Find the average mpg for size c

  avg = mpgbycyl / cylcount

# Add mpg average to list

avg_mpg.append ((c, avg))

 
# List sorting

< p> avg_mpg.sort (key = lambda x: x [ 0 ])

  
# Print list

print (avg_mpg)

Exit :


As we can see from the output, the program successfully returned a list of tuples containing mpg average for each unique cylinder type in our dataset.

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically