
- Python Pandas - Home
- Python Pandas - Introduction
- Python Pandas - Environment Setup
- Python Pandas - Basics
- Python Pandas - Introduction to Data Structures
- Python Pandas - Index Objects
- Python Pandas - Panel
- Python Pandas - Basic Functionality
- Python Pandas - Indexing & Selecting Data
- Python Pandas - Series
- Python Pandas - Series
- Python Pandas - Slicing a Series Object
- Python Pandas - Attributes of a Series Object
- Python Pandas - Arithmetic Operations on Series Object
- Python Pandas - Converting Series to Other Objects
- Python Pandas - DataFrame
- Python Pandas - DataFrame
- Python Pandas - Accessing DataFrame
- Python Pandas - Slicing a DataFrame Object
- Python Pandas - Modifying DataFrame
- Python Pandas - Removing Rows from a DataFrame
- Python Pandas - Arithmetic Operations on DataFrame
- Python Pandas - IO Tools
- Python Pandas - IO Tools
- Python Pandas - Working with CSV Format
- Python Pandas - Reading & Writing JSON Files
- Python Pandas - Reading Data from an Excel File
- Python Pandas - Writing Data to Excel Files
- Python Pandas - Working with HTML Data
- Python Pandas - Clipboard
- Python Pandas - Working with HDF5 Format
- Python Pandas - Comparison with SQL
- Python Pandas - Data Handling
- Python Pandas - Sorting
- Python Pandas - Reindexing
- Python Pandas - Iteration
- Python Pandas - Concatenation
- Python Pandas - Statistical Functions
- Python Pandas - Descriptive Statistics
- Python Pandas - Working with Text Data
- Python Pandas - Function Application
- Python Pandas - Options & Customization
- Python Pandas - Window Functions
- Python Pandas - Aggregations
- Python Pandas - Merging/Joining
- Python Pandas - MultiIndex
- Python Pandas - Basics of MultiIndex
- Python Pandas - Indexing with MultiIndex
- Python Pandas - Advanced Reindexing with MultiIndex
- Python Pandas - Renaming MultiIndex Labels
- Python Pandas - Sorting a MultiIndex
- Python Pandas - Binary Operations
- Python Pandas - Binary Comparison Operations
- Python Pandas - Boolean Indexing
- Python Pandas - Boolean Masking
- Python Pandas - Data Reshaping & Pivoting
- Python Pandas - Pivoting
- Python Pandas - Stacking & Unstacking
- Python Pandas - Melting
- Python Pandas - Computing Dummy Variables
- Python Pandas - Categorical Data
- Python Pandas - Categorical Data
- Python Pandas - Ordering & Sorting Categorical Data
- Python Pandas - Comparing Categorical Data
- Python Pandas - Handling Missing Data
- Python Pandas - Missing Data
- Python Pandas - Filling Missing Data
- Python Pandas - Interpolation of Missing Values
- Python Pandas - Dropping Missing Data
- Python Pandas - Calculations with Missing Data
- Python Pandas - Handling Duplicates
- Python Pandas - Duplicated Data
- Python Pandas - Counting & Retrieving Unique Elements
- Python Pandas - Duplicated Labels
- Python Pandas - Grouping & Aggregation
- Python Pandas - GroupBy
- Python Pandas - Time-series Data
- Python Pandas - Date Functionality
- Python Pandas - Timedelta
- Python Pandas - Sparse Data Structures
- Python Pandas - Sparse Data
- Python Pandas - Visualization
- Python Pandas - Visualization
- Python Pandas - Additional Concepts
- Python Pandas - Caveats & Gotchas
Python Pandas - Fixed-Width Text File
A fixed-width text file is a text file that stores the tabular data arranged in columns with a fixed number of characters, meaning each field occupies a specific width. Unlike delimited files (e.g., CSVs), these files rely on consistent column widths rather than separators like commas. This file format is commonly used in older systems or specific applications, where each piece of data starts and ends at specific positions.
In Python, the Pandas library offers a convenient method called read_fwf() for handling such file formats. This method allows you to read such data efficiently into DataFrames for analysis. In this tutorial, we will learn how to work with fixed-width text file using Pandas.
Pandas read_fwf() Method
The read_fwf() method in Pandas is designed for reading fixed-width text files. Its works like the Pandas read_csv() method, but instead of reading delimited data, it works with files where each column has a fixed width.
This method returns a Pandas DataFrame or a TextFileReader when iterator=True.
Following is the syntax of the this method −
pandas.read_fwf(filepath_or_buffer, *, colspecs='infer', widths=None, **kwds)
Key parameters are −
colspecs: A list of tuples specifying the start and end positions of each column. Set to 'infer' by default, which tries to deduce column widths from the first 100 rows.
widths: A list of integers specifying the width of each column. Use this for contiguous columns as an alternative to colspecs.
delimiter: Characters considered as fillers in the file, e.g., spaces or special characters like ~.
dtype: Allows manual specification of column data types.
**kwargs: Additional optional keyword arguments passed to TextFileReader.
Example
Here is a basic example demonstrating reading a fixed-width file using the pandas read_fwf() method with column specifications. In this example we explicitly define the column intervals, using the colspecs parameter.
import pandas as pd # Sample fixed-width data saved to a file data = """ Name Gender Age Braund Male 30 Cumings Female 25 Heikkinen Female 35 Tom Male 28 Ram Male 25""" # Writing data to a file with open("sample_fwf.txt", "w") as file: file.write(data) # Specifying column intervals colspecs = [(0, 11), (11, 20), (20, 26)] # Reading the fixed-width file df = pd.read_fwf("sample_fwf.txt", colspecs=colspecs) print("DataFrame from fixed-width file with specified column intervals:") print(df)
When we run above program, it produces following result −
DataFrame from fixed-width file with specified column intervals:
Name | Gender | Age | |
---|---|---|---|
0 | Braund | Male | 30 |
1 | Cumings | Female | 25 |
2 | Heikkinen | Female | 25 |
3 | Tom | Male | 28 |
4 | Ram | Male | 25 |
Inferring Column Specifications
Pandas read_fwf() method can automatically infer column positions if the data is aligned and separated by white space or other delimiters.
Example
The following example demonstrates using the read_fwf() method for automatically inferring the column specifications with the default parameter settings.
import pandas as pd # Reading the fixed-width file with default parameter settings df = pd.read_fwf("sample_fwf.txt") print("DataFrame from fixed-width file:") print(df)
Following is an output of the above code −
DataFrame from fixed-width file:
Name | Gender | Age | |
---|---|---|---|
0 | Braund | Male | 30 |
1 | Cumings | Female | 25 |
2 | Heikkinen | Female | 25 |
3 | Tom | Male | 28 |
4 | Ram | Male | 25 |
Handling Data Types in Fixed-Width File
You can also manually specify column data types while parsing the data from a fixed-width file using the dtype parameter of the read_fwf() method.
Example
In this example, the Age column is explicitly parsed as a float type instead of the default int type.
import pandas as pd # Reading the fixed-width file with specific data types for columns df = pd.read_fwf("sample_fwf.txt", dtype={"Age": "float"}) print("DataFrame from fixed-width file:") print(df) print("\nData Types of Each Column:") print(df.dtypes)
While executing the above code we get the following output −
DataFrame from fixed-width file:
Name | Gender | Age | |
---|---|---|---|
0 | Braund | Male | 30 |
1 | Cumings | Female | 25 |
2 | Heikkinen | Femate | 25 |
3 | Tom | Male | 28 |
4 | Ram | Male | 25 |
Reading the fixed-width file with a Custom Delimiter
The read_fwf() method allows you to read the fixed-width text file with custom delimiter using the delimiter parameter. This is useful for reading a fixed-width text file containing the delimiter other than white spaces.
Example
This example demonstrates reading a fixed-width file containing a custom delimiter other than the white space using the delimiter parameter. Here the "-" filler character(delimiter) will be ignored.
import pandas as pd # Sample fixed-width data saved to a file data = """ Name-------Gender---Age Braund-----Male-----30 Cumings----Female---25 Heikkinen--Female---35 Tom--------Male-----28 Ram--------Male-----25""" # Writing data to a file with open("sample_fwf.txt", "w") as file: file.write(data) # Reading the fixed-width file with Custom Delimiter df = pd.read_fwf("sample_fwf.txt", delimiter="-") print("DataFrame from fixed-width file with with custom delimiter:") print(df)
Following is an output of the above code −
DataFrame from fixed-width file with with custom delimiter:
Name | Gender | Age | |
---|---|---|---|
0 | Braund | Male | 30 |
1 | Cumings | Female | 25 |
2 | Heikkinen | Femate | 25 |
3 | Tom | Male | 28 |
4 | Ram | Male | 25 |
Iterating the Fixed-Width File Data
By specifying a chunksize or iterator=True parameters of the read_fwf() method, you can get an iterable object of type TextFileReader for iterating the data of a fixed-width file data in chunk.
Example
This example shows parsing the fixed-width file data in chunks by specifying the chunksize parameter. Here chunk size represents number of lines to read per chunk for iteration.
import pandas as pd # Reading the file in chunks chunk_size = 1 chunks = pd.read_fwf("sample_fwf.txt", chunksize=chunk_size) print("Iterating through Fixed-Width File Chunk by Chunk:") for chunk in chunks: print(chunk)
When we run above program, it produces following result −
Iterating through Fixed-Width File Chunk by Chunk:
Name | Gender | Age | |
---|---|---|---|
0 | Braund | Male | 30 |
Name | Gender | Age | |
---|---|---|---|
1 | Cumings | Female | 25 |
Name | Gender | Age | |
---|---|---|---|
2 | Heikkinen | Femate | 25 |
Name | Gender | Age | |
---|---|---|---|
3 | Tom | Male | 28 |
Name | Gender | Age | |
---|---|---|---|
4 | Ram | Male | 25 |