Reading and writing CSVs

Data Manipulation with pandas

Maggie Matsui

Senior Content Developer at DataCamp

What's a CSV file?

  • CSV = comma-separated values
  • Designed for DataFrame-like data
  • Most database and spreadsheet programs can use them or create them

CSV file type icon

Data Manipulation with pandas

Example CSV file

The dataset containing two new dogs

new_dogs.csv

name,breed,height_cm,weight_kg,d_o_b
Ginger,Dachshund,22,10,2019-03-14
Scout,Dalmatian,59,25,2019-05-09
Data Manipulation with pandas

CSV to DataFrame

import pandas as pd

new_dogs = pd.read_csv("new_dogs.csv")
print(new_dogs)
     name      breed  height_cm  weight_kg date_of_birth
0  Ginger  Dachshund         22         10    2019-03-14
1   Scout  Dalmatian         59         25    2019-05-09
Data Manipulation with pandas

DataFrame manipulation

new_dogs["bmi"] = new_dogs["weight_kg"] / (new_dogs["height_cm"] / 100) ** 2

print(new_dogs)
     name      breed  height_cm  weight_kg date_of_birth         bmi
0  Ginger  Dachshund         22         10    2019-03-14  206.611570
1   Scout  Dalmatian         59         25    2019-05-09   71.818443
Data Manipulation with pandas

DataFrame to CSV

new_dogs.to_csv("new_dogs_with_bmi.csv")

new_dogs_with_bmi.csv

name,breed,height_cm,weight_kg,d_o_b,bmi
Ginger,Dachshund,22,10,2019-03-14,206.611570
Scout,Dalmatian,59,25,2019-05-09,71.818443
Data Manipulation with pandas

Let's practice!

Data Manipulation with pandas

Preparing Video For Download...