Chapter 6. Data Loading, Storage, and File Formats
The tools in this book are of little use if you can’t easily import and export data in Python. I’m going to be focused on input and output with pandas objects, though there are of course numerous tools in other libraries to aid in this process. NumPy, for example, features low-level but extremely fast binary data loading and storage, including support for memory-mapped array. See Chapter 12 for more on those.
Input and output typically falls into a few main categories: reading text files and other more efficient on-disk formats, loading data from databases, and interacting with network sources like web APIs.
Reading and Writing Data in Text Format
Python has become a beloved language for text and file munging due to its simple syntax for interacting with files, intuitive data structures, and convenient features like tuple packing and unpacking.
pandas features a number of functions for reading tabular data as a
DataFrame object. Table 6-1 has a summary
of all of them, though read_csv
and read_table
are likely the
ones you’ll use the most.
Table 6-1. Parsing functions in pandas
Function | Description |
---|---|
read_csv | Load delimited data from a file, URL, or file-like object. Use comma as default delimiter |
read_table | Load delimited data from a file, URL, or file-like object.
Use tab ('\t' ) as default
delimiter |
read_fwf | Read data in fixed-width column format (that is, no delimiters) |
read_clipboard | Version of read_table that reads data from the clipboard. Useful ... |
Get Python for Data Analysis now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.