Movatterモバイル変換


[0]ホーム

URL:


Python Pandas Tutorial

Python Pandas read_feather() Method



Theread_feather() method in Python's Pandas library allows you to read/load the feather-format object from the file path into a Pandas object, enabling fast and efficient data retrieval.

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

Before using the read_feather() method, please ensure that the 'pyarrow' library is installed, as it is an optional dependency. We need to install it by using the following command −

pip install pyarrow

Syntax

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

pandas.read_feather(path, columns=None, use_threads=True, storage_options=None, dtype_backend=<no_default>)

Parameters

The Python Pandasread_feather() method accepts the below parameters −

  • path: This method accepts a string, path object, or file-like object that specifies the file path. It can be a local file path or a remote URL supporting schemes like http, ftp, s3, and file. For local files, the expected format isfile://localhost/path/to/file.feather.

  • columns: Specifies which columns to read. If not provided, all columns are read.

  • use_threads: Specifies whether to parallelize reading using multiple threads.

  • storage_options: Additional options for storage connections, such as host, port, username, password, etc.

  • dtype_backend: Specifies the data type backend for the resulting DataFrame.

Return Value

The Pandasread_feather() method returns the same type of Pandas object that was stored in the feather format file.

Example: Loading a Feather File into Pandas Object

Here is a basic example demonstrating loading a Pandas DataFrame object from a feather file using the Pandasread_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")# Load the DataFrame from the feather fileresult = pd.read_feather("df_feather_file.feather")print("DataFrame Loaded from Feather File:")print(result)

When we run above program, it produces following result −

Original DataFrame:
Col_1Col_2
005
116
227
338
449
DataFrame Loaded from Feather File:
Col_1Col_2
005
116
227
338
449

Example: Reading Selected Columns from a Feather File

This example loads the Pandas DataFrame with the selected columns from a feather file using the Pandasread_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")# Read only specific columnsdf = pd.read_feather('df_feather_file.feather', columns=['Col_1'])print("DataFrame with Selected Column:")print(df)

While executing the above code we get the following output −

Original DataFrame:
Col_1Col_2
005
116
227
338
449
DataFrame with Selected Column:
Col_1
00
11
22
33
44

Example: Loading a feather File with Compression

The following example initially saves a compressed feather file and then loads it using theread_feather() method to read the compressed feather file back into the Pandas object.

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..")# Load the compressed filecompressed_df = pd.read_feather("compressed_data.feather")print("\nLoaded Compressed DataFrame:")print(compressed_df)

Following is an output of the above code −

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

Example: Reading from an In-Memory Feather File

Theread_feather() method can also read the In-Memory feather file. Here is an example demonstrates the same.

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)# Read the DataFrame from the in-memory bufferloaded_df = pd.read_feather(buf)print("DataFrame Loaded from In-Memory Feather:")print(loaded_df)

Following is an output of the above code −

Original DataFrame:
Col_1Col_2
013.0
124.0
DataFrame Loaded from In-Memory Feather:
Col_1Col_2
013.0
124.0
python_pandas_io_tool.htm
Print Page
Advertisements

[8]ページ先頭

©2009-2025 Movatter.jp