Archive
Python and Excel P1
Learning : Python and Excel Part-1
Subject: Read Excel file
In the coming four or five Lessons we will work on reading and writing to Excel file using Python code.
First we need to import os and Pandas as in coming code block, I will use os later to fetch file information. Also we will set the file_name variable to our file assuming it is in the same .py directory and we will call [read] the file in to df [dataframe]. Here is the code
# Import and call the File into DataFrame. import os, pandas as pd file_name = 'python_sheet_4.xlsx' df = pd.read_excel(file_name, sheet_name='Sheet1')
Now let’s talk about my excel file “python_sheet_4.xlsx” has two sheets named “sheet1 and sheet2” both contin same table sheet1 the table start from cell A1 and in sheet2 the table start from cell C7. So first we will work on sheet1 that’s way we call sheet1 in our df setting statement.
Now we have the df (dataframe), and if we run the this code:
# To print out the data from df DataFrame. print(df)
The Output..![]() |
Here is the original shot for our excel table
![]() |
Here is a list of commands that we can use..
To make things in a professional way, we will write a function so we can use it with other applications, here is it..
The Code![]() |
[Out-Put]:![]() |
To Download my Python code (.py) files Click-Here
By: Ali Radwani