Copy Rows and Columns in Excel Using Python
Last Updated :
28 Apr, 2025
Manipulating data in Excel often involves tasks such as copying specific rows or columns based on certain conditions. Python, with its powerful libraries like P
andas and openpyxl
, provides efficient solutions for handling Excel files. In this article, we will explore how to copy rows and columns in Excel using Python, showcasing practical examples for different scenarios.
What is Openpyxl?
Openpyxl
is a Python library for reading and writing Excel (xlsx) files. It allows developers to work with Excel files, manipulate data, formatting, and perform various operations programmatically. openpyxl
provides a convenient and Pythonic way to interact with Excel files, making it easy to handle tasks like creating new workbooks, modifying existing ones, and extracting or inserting data.
Copy Rows and Columns in Excel Using Python
Below, are examples of how to Copy Rows And Columns In Excel Using perform. First, install the openpyxl
library, which is essential for copying rows and columns in Excel using Python, use the following command:
pip install openpyxl
Excel1.xlsx:
excel1.xlsxExample 1: Copy Specific Rows in Excel Using Python
In this example, below Python code uses the Pandas
library to read data from an Excel file named 'excel1.xlsx' into a DataFrame. It then filters rows based on a specified condition where the 'Salary' column is greater than $50,000. The resulting DataFrame is saved as a new Excel file named 'excel2.xlsx', excluding the index column.
Python3
import pandas as pd
# Read the Excel file
df = pd.read_excel('excel1.xlsx')
# Copy rows based on condition
condition = df['Salary'] > 50000
result = df[condition]
# Save the result to a new Excel file
result.to_excel('excel2.xlsx', index=False)
Output (excel2.xlsx) :
excel2.xlsxExample 2: Copy Specific Columns in Excel Using Python
In this example, below Python code, uses Pandas
library, reads data from an Excel file named 'excel1.xlsx' into a DataFrame. It then selects specific columns ('Name' and 'Age') based on a condition where the 'Age' column is less than 30. The resulting DataFrame is saved as a new Excel file named 'excel2.xlsx', excluding the index column.
Python3
import pandas as pd
# Read the Excel file
df = pd.read_excel('excel1.xlsx')
# Copy columns based on condition
condition = df['Age'] < 30
result = df.loc[condition, ['Name', 'Age']]
# Save the result to a new Excel file
result.to_excel('excel2.xlsx', index=False)
Output (excel2.xlsx):
excel2.xlsxExample 3: Copy Complete Content From One File to Another
In this example, below code using the openpyxl
library, the code loads data from the source Excel file 'excel1.xlsx' into a workbook and worksheet. It then attempts to create a new Excel file 'excel1.xlsx' for the destination, which might be unintentional. The script iterates through rows in the source worksheet and appends them to the destination worksheet.
Python3
from openpyxl import load_workbook
# Load the source Excel file
source_wb = load_workbook('excel1.xlsx')
source_ws = source_wb.active
# Create a new Excel file
destination_wb = load_workbook('excel1.xlsx')
destination_ws = destination_wb.active
# Copy data from source to destination
for row in source_ws.iter_rows(min_row=1, max_row=source_ws.max_row, values_only=True):
destination_ws.append(row)
# Save the destination file
destination_wb.save('excel2.xlsx')
Output (excel2.xlsx)
excel2.xlsx
Similar Reads
How to Append Data in Excel Using Python We are given an Excel file and our task is to append the data into this excel file using Python. In this article, we'll explore different approaches to append data to an Excel file using Python. Append Data in Excel Using PythonBelow, are some examples to understand how to append data in excel using
2 min read
Cloning Row and Column Vectors in Python In Python, cloning row or column vectors involves creating a duplicate copy of a one-dimensional array (vector) either horizontally (row) or vertically (column). Cloning vectors is important for preserving data integrity and avoiding unintended modifications to the original array. In this article, w
2 min read
Convert Excel To Json With Python In the realm of data manipulation and analysis, the ability to convert data between different formats is a valuable skill. Excel and JSON are two widely used formats, and Python, with its powerful libraries, provides a seamless way to convert Excel files into JSON. In this article, we will see how w
4 min read
How to Add Numbers in a Csv File Using Python When working with CSV files in Python, adding numbers in the CSV file is a common requirement. This article will guide you through the process of adding numbers within a CSV file. Whether you're new to data analysis or an experienced practitioner, understanding this skill is vital for efficient data
3 min read
Exporting Multiple Sheets As Csv Using Python In data processing and analysis, spreadsheets are a common format for storing and manipulating data. However, when working with large datasets or conducting complex analyses, it's often necessary to export data from multiple sheets into a more versatile format. CSV (Comma-Separated Values) files are
3 min read
Fastest Way to Read Excel File in Python Reading Excel files is a common task in data analysis and processing. Python provides several libraries to handle Excel files, each with its advantages in terms of speed and ease of use. This article explores the fastest methods to read Excel files in Python.Using pandaspandas is a powerful and flex
3 min read