In this tutorial, we will learn how to extract two columns from an Excel file using Python. For this purpose, we will use the popular Python library pandas. Pandas provides an easy and powerful way of dealing with Excel files and accomplishing our desired tasks without much hassle.
This tutorial will guide you step-by-step on how to extract data from an Excel file and perform operations on that data.
Step 1: Create Excel file “example.xlsx”
ColumnA ColumnB ColumnC 1 A 100 2 B 200 3 C 300 4 D 400 5 E 500
Step 2: Read the Excel file
Next, we will use the read_excel()
function from pandas to load the Excel file into a DataFrame. You need to provide the file path in the function argument like this:
1 |
data = pd.read_excel("example.xlsx") |
Replace 'example.xlsx'
with the path to your Excel file.
Step 3: Extracting two columns
Now that we have loaded the data, we can use the DataFrame to easily extract the desired columns. Let’s say we want to extract two columns named ‘ColumnA’ and ‘ColumnB’:
1 |
columns_to_extract = ['ColumnA', 'ColumnB']<br>extracted_data = data[columns_to_extract] |
Step 4: Saving the extracted data to a new Excel file
We can save the extracted data into another Excel file using the to_excel()
function. The following code will save the DataFrame to a new Excel file named ‘extracted_columns.xlsx’:
1 |
extracted_data.to_excel('extracted_columns.xlsx', index=False) |
Setting index=False
will exclude the index column from the output file.
Now that we have learned the steps to extract two columns from an Excel file in Python let’s see the full code in action.
Full Code:
1 2 3 4 5 6 7 8 |
import pandas as pd data = pd.read_excel("example.xlsx") columns_to_extract = ['ColumnA', 'ColumnB'] extracted_data = data[columns_to_extract] extracted_data.to_excel('extracted_columns.xlsx', index=False) |
Output Excel file “extracted_columns.xlsx”
ColumnA ColumnB 1 A 2 B 3 C 4 D 5 E
Conclusion
In this tutorial, we learned how to extract two specific columns from an Excel file using the pandas library in Python. The steps are straightforward and can be extended for more columns or other operations as needed. This tutorial demonstrated the ease of working with Excel files in Python, making data manipulation simple and efficient.