import pandas as pd
df = pd.read_excel('D:\\student.xlsx') # Use your path to point to file
print(df.head()) # top five rows of the DataFrame
Output is here
id name class mark gender
0 1 John Deo Four 75 female
1 2 Max Ruin Three 85 male
2 3 Arnold Three 55 male
3 4 Krish Star Four 60 female
4 5 John Mike Four 60 female
Pandas sample DataFrame ( student )
df=pd.read_excel('student.xlsx',sheet_name='student'))
Now we can read the data of my_Sheet_2 by using above code, sheet_name=0 is for reading the data of first sheet i.e my_Sheet_1. We can also use the sheet names.
df = pd.read_excel('D:\\student.xlsx', sheet_name='my_Sheet_1')
By default it reads the first sheet or sheet_name=0
import pandas as pd
df=pd.read_excel('student.xlsx',index_col='id')
print(df.head())
df = pd.read_excel('D:\\student.xlsx',header=1)
df = pd.read_excel('D:\\student.xlsx',header=None)
0 1 2 3 4
0 id name class mark gender
1 1 John Deo Four 75 female
2 2 Max Ruin Three 85 male
3 3 Arnold Three 55 male
4 4 Krish Star Four 60 female
As the number are used as headers , we can create our own names by using a list.
import pandas as pd
my_list=['header1','header2','header3','header4','header5']
df = pd.read_excel('D:\\student.xlsx',header=None,names=my_list)
print(df)
Output is here
header1 header2 header3 header4 header5
0 id name class mark gender
1 1 John Deo Four 75 female
2 2 Max Ruin Three 85 male
3 3 Arnold Three 55 male
4 4 Krish Star Four 60 female
df = pd.read_excel('C:\\student.xlsx',usecols=['id','name'])
Here id, name and mark columns are used
df = pd.read_excel('C:\\student.xlsx',usecols=[0,1,3])
df = pd.read_excel('C:\\student.xlsx',dtype={'mark':float})
Use info() to get the dtype of the columns.
print(df.info())
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 id 35 non-null int64
1 name 35 non-null object
2 class 35 non-null object
3 mark 35 non-null float64
4 gender 35 non-null object
dtypes: float64(1), int64(1), object(3)
memory usage: 1.5+ KB
df = pd.read_excel('E:\\PAYMENTS.xlsx', converters= {'DATE_column': pd.to_datetime})
We can apply any function to values in any column while creating the DataFrame. Here we are increasing the mark column values by 10.
df = pd.read_excel('C:\\student.xlsx',converters={'mark': lambda x:x+10})
Note that we have used a dictionary as input to the converters. my_dict={'mark': lambda x:x+2,'name':lambda x:'Mr '+x}
df = pd.read_excel('C:\\student.xlsx',converters=my_dict)
print(df.head()
Output
id name class mark gender
0 1 Mr John Deo Four 77 female
1 2 Mr Max Ruin Three 87 male
2 3 Mr Arnold Three 57 male
3 4 Mr Krish Star Four 62 female
4 5 Mr John Mike Four 62 female
df = pd.read_excel('C:\\student.xlsx',skiprows=[2,3])
Don't use first 6 rows ( 0 indexed )
df = pd.read_excel('C:\\student.xlsx',skiprows=6)
Don't use even number of rows. ( using callable function )
df = pd.read_excel('C:\\student.xlsx',skiprows=lambda x: True if x % 2 == 0 else False)
Output
1 John Deo Four 75 female
0 3 Arnold Three 55 male
1 5 John Mike Four 60 female
2 7 My John Rob Five 78 male
3 9 Tes Qry Six 78 male
4 11 Ronald Six 89 female
df = pd.read_excel('C:\\student.xlsx',nrows=2)
df = pd.read_excel('C:\\student.xlsx',skipfooter=33)
import pandas as pd
df = pd.read_excel('D:\student.xlsx')
my_classes=df['class'].unique()
print(my_classes)
Output is a list
['Four' 'Three' 'Five' 'Six' 'Seven' 'Nine' 'Eight']
import pandas as pd
my_dict={
'NAME':['Ravi','Raju','Alex'],
'ID':[1,2,3],'MATH':[30,40,50],
'ENGLISH':[20,30,40]
}
my_dict2={
'NAME':['Ron','Pick','Rabin'],
'ID':[4,5,6],'MATH':[60,40,30],
'ENGLISH':[30,40,50]
}
df = pd.DataFrame(data=my_dict)
df2 = pd.DataFrame(data=my_dict2)
with pd.ExcelWriter('D:my_file.xlsx') as my_excel_obj: #Object created
df.to_excel(my_excel_obj,sheet_name='my_Sheet_1')
df2.to_excel(my_excel_obj,sheet_name='my_Sheet_2')
import tkinter as tk
from tkinter import *
from tkinter import filedialog
import pandas as pd
my_w = tk.Tk()
my_w.geometry("410x300") # Size of the window
my_w.title('www.plus2net.com')
my_font1=('times', 18, 'bold')
l1 = tk.Label(my_w,text='Create DataFrame',width=30,font=my_font1)
l1.grid(row=0,column=1)
b1 = tk.Button(my_w, text='Upload Excel File',
width=20,command = lambda:upload_file())
b1.grid(row=1,column=1)
t1 = tk.Text(my_w, height=7, width=45,bg='yellow') # added one text box
t1.grid(row=2,column=1,pady=10) #
def upload_file():
file = filedialog.askopenfilename(
filetypes=[("Excel file", ".xlsx")])
df=pd.read_excel(file,index_col='id') # creating DataFrame
t1.delete('1.0',END) # Delete previous data from position 0 till end
t1.insert(tk.END, df.head()) # adding data to text widget
my_w.mainloop() # Keep the window open
import tkinter as tk
from tkinter import *
from tkinter import filedialog
import pandas as pd
from sqlalchemy import create_engine
from sqlalchemy.exc import SQLAlchemyError
#my_conn = create_engine("sqlite:///E:\\testing\\sqlite\\my_db4.db") # SQLite
my_conn =create_engine("mysql+mysqldb://u_id:pw@localhost/my_tutorial")
my_w = tk.Tk()
my_w.geometry("400x300") # Size of the window
my_w.title('www.plus2net.com')
l1 = tk.Label(my_w,text='Upload File & Add to Database',width=30,font=18)
l1.grid(row=1,column=1,padx=5,pady=20)
b1 = tk.Button(my_w, text='Upload File',
width=20,command = lambda :upload_file())
b1.grid(row=2,column=1)
def upload_file():
f_types = [('All Files', '*.*'),
('Excel files', '*.xlsx'),
('Text Document', '*.txt'),
('CSV files',"*.csv")]
path = filedialog.askopenfilename(filetypes=f_types)
if path:
my_upd(path)
def my_upd(file):
df = pd.read_excel(file)
### Creating new table my_table or appending existing table
try:
df.to_sql(con=my_conn,name='my_table',if_exists='append') #DataFrame
except SQLAlchemyError as e:
#print(e)
error = str(e.__dict__['orig'])
print(error)
else: # No error
l1.config(text='Data added to table')
my_w.mainloop() # Keep the window open
import pandas as pd
df=pd.read_excel("D:\\my_data\\tk-colours.xlsx") # Create DataFrame.
my_list=df['Name'].values.tolist() # list with column data
import pandas as pd
df = pd.read_excel('E:\\data\\student.xlsx',index_col='id') # Excel path
#print(df.loc[id].tolist()) # as list
import tkinter as tk
my_w = tk.Tk()
my_w.geometry("300x200") # with and height of the window
my_w.title("plus2net.com") # Adding a title
l1 = tk.Label(my_w, text='ID', width=5,font=18) # added one Label
l1.grid(row=1,column=1,padx=2,pady=10)
e1 = tk.Entry(my_w, width=5,bg='yellow',font=18) # added one Entry box
e1.grid(row=1,column=2,padx=2)
b1=tk.Button(my_w,text='GO', width=5,font=18,command=lambda:my_upd())
b1.grid(row=1,column=3,padx=5)
l2 = tk.Label(my_w, text='Details', font=10) # added one Label
l2.grid(row=2,column=1,padx=2,pady=10,columnspan=3)
def my_upd():
id=int(e1.get()) # read the user entered data or id
l2.config(text=df.loc[id]) # update the Label
my_w.mainloop()
import pandas as pd
df = pd.read_excel("D:\\student.xlsx")
#print(df)
for index, row in df.iterrows():
print(""+ row['name']+" "+ row['class']+" "+str(row['mark'])+" "+row['gender']+" ")
read_excel()
function in Pandas?sheet_name
parameter in the read_excel()
function?read_excel()
function?header
parameter in the read_excel()
function?read_excel()
function?read_excel()
function?read_excel()
function?dtype
parameter in the read_excel()
function?read_excel()
function?read_excel()
function?Author
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.