Pandas DataFrame to_feather() Method


Preparation

Before any data manipulation can occur, three (3) new libraries will require installation.

  • The Pandas library enables access to/from a DataFrame.
  • The Pyarrow library allows writing/reading access to/from a parquet file.
  • The Openpyxl library allows styling/writing/reading to/from an Excel file.

To install these libraries, navigate to an IDE terminal. At the command prompt ($), execute the code below. For the terminal used in this example, the command prompt is a dollar sign ($). Your terminal prompt may be different.

$ pip install pandas

Hit the <Enter> key on the keyboard to start the installation process.

$ pip install pyarrow

Hit the <Enter> key on the keyboard to start the installation process.

$ pip install openpyxl

Hit the <Enter> key on the keyboard to start the installation process.

If the installations were successful, a message displays in the terminal indicating the same.


Feel free to view the PyCharm installation guide for the required libraries.


Add the following code to the top of each code snippet. This snippet will allow the code in this article to run error-free.

import pandas as pd
import pyarrow
import openpyxl

DataFrame.to_feather()

The to_feather() method writes a DataFrame object to a binary Feather format. This format is a lightweight and fast binary way to store a DataFrame. In addition, it takes up less space than an equivalent CSV file.

The syntax for this method is as follows:

DataFrame.to_feather(path, **kwargs)

Here’s a description of the parameters:

ParameterDescription
pathThis parameter is the string path to write. If empty, a string returns.
**kwargsAdditional parameters for the pyarrow library.

This example reads in the first five (5) rows from a semi-colon (;) delimited CSV file (cars.csv).

df = pd.read_csv('cars.csv', sep=';', usecols=['Name', 'MPG', 'Model']).head()
df.to_feather('cars.feather')
df = pd.read_feather('cars.feather')
print(df)
  • Line [1] reads in the first five (5) rows and three (3) columns from the CSV file. The output saves to df.
  • Line [2] converts the DataFrame to a Feather file (cars.feather).
  • Line [3] reads the Feather file (cars.feather) into a DataFrame.
  • Line [4] outputs the DataFrame to the terminal.

Output – cars.feather

NameMPGModel
0Chevrolet Chevelle Malibu18.070
1Buick Skylark 32015.070
2Plymouth Satellite18.070
3AMC Rebel SST16.070
4Ford Torino17.070

More Pandas DataFrame Methods

Feel free to learn more about the previous and next pandas DataFrame methods (alphabetically) here:

Also, check out the full cheat sheet overview of all Pandas DataFrame methods.