In this tutorial, we will learn how to read a particular cell value from an Excel file using Pandas in Python.
Pandas is a popular data manipulation library that can easily read, manipulate, and write data in a variety of formats including CSV, JSON, SQL, and Excel. It offers a wealth of operations to manipulate data efficiently and is often used in data analysis and cleaning.
Step 1: Create an Excel file
This is an example file called “filename.xlsx”.
Apple | 100 | $0.50 | $50.00 |
Banana | 75 | $0.40 | $30.00 |
Orange | 50 | $0.60 | $30.00 |
Step 2: Import the Pandas Library
Once you’ve installed the necessary libraries, you can import Pandas into your Python script:
1 |
import pandas as pd |
Step 3: Read the Excel File
Use the read_excel() method provided by Pandas to read the Excel file. This method returns a DataFrame that contains the data from the Excel file.
1 |
df = pd.read_excel('filename.xlsx', engine='openpyxl') |
Step 4: Read a Specific Cell Value
Now that you’ve read your Excel file into a DataFrame, you can use DataFrame’s iat[] or iloc[] method to retrieve a specific cell value by its position.
1 |
value = df.iat[1, 2] |
In the above code, 1 represents the row index and 2 represents the column index. Pandas uses zero-based indexing, so the row index and column index start from 0.
Full Code:
1 2 3 4 5 6 7 8 9 |
import pandas as pd # Read excel file df = pd.read_excel('filename.xlsx', engine='openpyxl') # Get specific cell value value = df.iat[1, 2] print(value) |
0.6
To read more about reading and writing Excel files in Pandas, visit the official Pandas documentation.
Conclusion
Reading a particular cell value from an Excel file can be done efficiently using the Pandas library in Python. By leveraging the powerful data manipulation capabilities of Pandas, you can easily work with Excel data in your Python scripts.