Python Pandas to_feather() Method



The to_feather() method in Pandas allows you to save DataFrame data into a binary feather format file.

The feather file format is a portable file format for saving the DataFrame. It is a fast and language-independent binary file format designed for efficient data interchange. It is supported by both Python and R languages, ensuring easy data sharing compatibility across data analysis languages. This format is also efficient for fast reading and writing capabilities with less memory usage.

Note: Before using the DataFrame.to_feather() method you need to install the 'pyarrow' library. It is an optional Python dependency library that must be installed using the following command −

pip install pyarrow

Syntax

Following is the syntax of the Python Pandas to_feather() method −

DataFrame.to_feather(path, **kwargs)

Parameters

The Python Pandas DataFrame.to_feather() method accepts the below parameters −

  • path − This parameter accepts a string, path object, or file-like object, representing the file path where the DataFrame should be saved.

  • **kwargs: Additional keyword arguments supported by pyarrow.feather.write_table() method, such as the compression, compression_level, chunksize and version keywords.

Return Value

The Pandas DataFrame.to_feather() method returns None, but saves the DataFrame as a feather file at the specified path.

Example: Saving a DataFrame to a Feather File

Here is a basic example demonstrating how to save a Pandas DataFrame as a Feather file using the DataFrame.to_feather() method.

import pandas as pd

# Create a DataFrame
df = pd.DataFrame({"Col_1": range(5), "Col_2": range(5, 10)})
print("Original DataFrame:")
print(df)

# Save the DataFrame as a feather file
df.to_feather("df_feather_file.feather")

print("\nDataFrame is successfully saved as a feather file.")

When we run above program, it produces following result −

Original DataFrame:
Col_1 Col_2
0 0 5
1 1 6
2 2 7
3 3 8
4 4 9
DataFrame is successfully saved as a feather file.
If you visit the folder where the feather files are saved, you can observe the generated feather file.

Example: Saving feather file with Compression

The following example shows how to use the to_feather() method for saving the Pandas DataFrame as a feather file with compression.

import pandas as pd

# Create a DataFrame
df = pd.DataFrame({"Col_1": range(5), "Col_2": range(5, 10)})
print("Original DataFrame:")
print(df)

# Save the DataFrame to a feather file with compression
df.to_feather('compressed_data.feather', compression='zstd')
print("DataFrame saved with compression..")

Following is an output of the above code −

Original DataFrame:
Col_1 Col_2
0 0 5
1 1 6
2 2 7
3 3 8
4 4 9
DataFrame saved with compression

Example: Saving feather with Feather Version 1

The DataFrame.to_feather() method also accepts a version parameter for changing the default feather file version 2 to 1.

import pandas as pd

# Create a DataFrame
df = pd.DataFrame({"Col_1": [1, 2, 3, 4, 5],
"Col_2": ["a", "b", "c", "d", "e"]})

print("Original DataFrame:")
print(df)

# Save the DataFrame to a feather file with version 1
df.to_feather('df_feather_file_v1.feather',  version=1)
print("DataFrame saved with Feather version 1..")

Following is an output of the above code −

Original DataFrame:
Col_1 Col_2
0 1 a
1 2 b
2 3 c
3 4 d
4 5 e
DataFrame saved with Feather version 1..

Example: Save Pandas DataFrame to In-Memory Feather

This example saves a Pandas DataFrame as an in-memory feather file using the DataFrame.to_feather() method.

import pandas as pd
import io

# Create a Pandas DataFrame 
df = pd.DataFrame(data={'Col_1': [1, 2], 'Col_2': [3.0, 4.0]})

# Display the Input DataFrame
print("Original DataFrame:")
print(df)

# Save the DataFrame as In-Memory feather
buf = io.BytesIO()
df.to_feather(buf)

output = pd.read_feather(buf)
print('Saved DataFrame as In-Memory feather:')
print(output)

While executing the above code we get the following output −

Original DataFrame:
Col_1 Col_2
0 1 3.0
1 2 4.0
Saved DataFrame as In-Memory feather:
Col_1 Col_2
0 1 3.0
1 2 4.0
python_pandas_io_tool.htm
Advertisements