Pandas read excel line by line

Pandas: Read Excel Line by Line

Pandas is a powerful library in Python for data manipulation and analysis. The read_excel() function in Pandas allows us to read data from an Excel file. By default, it reads the entire Excel file into memory. However, if the Excel file is large, it may not be efficient to load the entire file at once. In such cases, we may need to read the Excel file line by line.

Here is an example of how to read an Excel file line by line using Pandas:

<pre>
import pandas as pd

file_path = "data.xlsx"
sheet_name = "Sheet1"

excel_data = pd.read_excel(file_path, sheet_name=sheet_name, header=None)

for index, row in excel_data.iterrows():
# Process each row
print(f"Row: {index+1}")
print(row)
</pre>

In this example, we first import the necessary libraries – Pandas. Then, we specify the path of the Excel file using the file_path variable and the name of the sheet using the sheet_name variable.

We use the read_excel() function to read the Excel file data into a Pandas DataFrame. We set the header parameter to None so that Pandas does not assume the first row as column names.

We then iterate over each row in the DataFrame using the iterrows() function, which returns an iterator containing the index and row contents. We can process each row as needed. In the given example, we print the index and row data.

This is just a basic example, and you can modify it according to your specific requirements. You can perform various operations on the data within the loop, such as filtering, transforming, or writing to another file.

Leave a comment