9.12.2 Reading CSV Files into Pandas DataFrames

  • Here, we demonstrate pandas’ ability to load files in CSV format, then perform some basic data-analysis tasks

Datasets

Working with Locally Stored CSV Files

  • File we'll process in this example
In [1]:
!cat accounts.csv
100,Jones,24.98
200,Doe,345.67
300,White,0.0
400,Stone,-42.16
500,Rich,224.62
  • Load a CSV dataset into a DataFrame with the pandas function read_csv
  • names argument specifies the DataFrame’s column names
    • Without this argument, read_csv assumes that the CSV file’s first row is a comma-delimited list of column names
In [2]:
import pandas as pd
In [3]:
df = pd.read_csv('accounts.csv', 
                 names=['account', 'name', 'balance'])
In [4]:
df
Out[4]:
account name balance
0 100 Jones 24.98
1 200 Doe 345.67
2 300 White 0.00
3 400 Stone -42.16
4 500 Rich 224.62
  • To save a DataFrame to a file using CSV format, call DataFrame method to_csv
  • index=False indicates that the row names (04 at the left of the DataFrame’s output above are not written to the file
  • Resulting file contains the column names as the first row
In [5]:
df.to_csv('accounts_from_dataframe.csv', index=False)
In [6]:
!cat accounts_from_dataframe.csv
account,name,balance
100,Jones,24.98
200,Doe,345.67
300,White,0.0
400,Stone,-42.16
500,Rich,224.62

©1992–2020 by Pearson Education, Inc. All Rights Reserved. This content is based on Chapter 5 of the book Intro to Python for Computer Science and Data Science: Learning to Program with AI, Big Data and the Cloud.

DISCLAIMER: The authors and publisher of this book have used their best efforts in preparing the book. These efforts include the development, research, and testing of the theories and programs to determine their effectiveness. The authors and publisher make no warranty of any kind, expressed or implied, with regard to these programs or to the documentation contained in these books. The authors and publisher shall not be liable in any event for incidental or consequential damages in connection with, or arising out of, the furnishing, performance, or use of these programs.