Movatterモバイル変換


[0]ホーム

URL:


Python Pandas Tutorial

Python Pandas to_feather() Method



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

Thefeather 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 theDataFrame.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 PandasDataFrame.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 bypyarrow.feather.write_table() method, such as the compression, compression_level, chunksize and version keywords.

Return Value

The PandasDataFrame.to_feather() method returnsNone, 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 theDataFrame.to_feather() method.

import pandas as pd# Create a DataFramedf = pd.DataFrame({"Col_1": range(5), "Col_2": range(5, 10)})print("Original DataFrame:")print(df)# Save the DataFrame as a feather filedf.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_1Col_2
005
116
227
338
449
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 theto_feather() method for saving the Pandas DataFrame as a feather file with compression.

import pandas as pd# Create a DataFramedf = pd.DataFrame({"Col_1": range(5), "Col_2": range(5, 10)})print("Original DataFrame:")print(df)# Save the DataFrame to a feather file with compressiondf.to_feather('compressed_data.feather', compression='zstd')print("DataFrame saved with compression..")

Following is an output of the above code −

Original DataFrame:
Col_1Col_2
005
116
227
338
449
DataFrame saved with compression

Example: Saving feather with Feather Version 1

TheDataFrame.to_feather() method also accepts aversion parameter for changing the default feather file version 2 to 1.

import pandas as pd# Create a DataFramedf = 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 1df.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_1Col_2
01a
12b
23c
34d
45e
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 theDataFrame.to_feather() method.

import pandas as pdimport io# Create a Pandas DataFrame df = pd.DataFrame(data={'Col_1': [1, 2], 'Col_2': [3.0, 4.0]})# Display the Input DataFrameprint("Original DataFrame:")print(df)# Save the DataFrame as In-Memory featherbuf = 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_1Col_2
013.0
124.0
Saved DataFrame as In-Memory feather:
Col_1Col_2
013.0
124.0
python_pandas_io_tool.htm
Print Page
Advertisements

[8]ページ先頭

©2009-2025 Movatter.jp