
This project uses a Tkinter file browser to select a CSV file and Pandas read_csv() to create a DataFrame.
After the CSV file is loaded, the program displays the number of rows and columns using the DataFrame shape attribute. The records are then shown in a scrollable Treeview.
Browse CSV file
|
v
pd.read_csv()
|
v
Pandas DataFrame
|
+--> df.shape
|
+--> column names
|
+--> rows
|
v
Treeview
Show Table of Contents
The file dialog lets the user browse the computer instead of hardcoding a CSV filename.
from tkinter import filedialog
file_path=filedialog.askopenfilename(
title='Select CSV file',
filetypes=[
('CSV files','*.csv'),
('All files','*.*')
]
)
If the user closes the dialog without selecting a file, askopenfilename() returns an empty string.
if not file_path:
status_var.set(
'No file selected.'
)
return
The selected filename is passed directly to Pandas.
df=pd.read_csv(
file_path
)
The DataFrame now contains the CSV columns and rows.
Because a CSV may be empty, incorrectly formatted or use an unexpected text encoding, the revised application also handles common read errors.
try:
df=pd.read_csv(
file_path
)
except (
OSError,
UnicodeDecodeError,
pd.errors.ParserError,
pd.errors.EmptyDataError
) as e:
status_var.set(
f'Unable to read CSV: {e}'
)
return
The DataFrame shape attribute returns:
(number_of_rows, number_of_columns)
For example:
rows=df.shape[0]
columns=df.shape[1]
The information can be displayed in a Tkinter Label:
info_var.set(
f'Rows: {df.shape[0]} Columns: {df.shape[1]}'
)

A Treeview is useful for displaying DataFrame data in rows and columns.
Because different CSV files can contain different column names, this project creates the Treeview headings dynamically. See also our dynamic Treeview columns and headings tutorial.
It is better not to use the actual DataFrame column names as Treeview internal identifiers. Column names can contain spaces, symbols or other values that are inconvenient as widget identifiers.
Instead, create safe internal IDs:
column_ids=[
f'c{i}'
for i in range(
len(df.columns)
)
]
If the CSV contains:
Name
Class
Mark
Admission Date
Treeview internally uses:
c0
c1
c2
c3
while the original DataFrame headings remain visible to the user.
for column_id,column_name in zip(
column_ids,
df.columns
):
tree.heading(
column_id,
text=str(column_name)
)
The older example used the first value in each CSV row as the Treeview iid.
iid=v[0]
This is unreliable because the first CSV column may contain duplicate values.
The revised program lets Treeview create its own unique item IDs:
tree.insert(
'',
tk.END,
values=values
)
Rows can be read efficiently from the DataFrame using:
for row in df.itertuples(
index=False,
name=None
):
...
Missing values are displayed as empty Treeview cells:
values=[
'' if pd.isna(value)
else value
for value in row
]
A CSV file can contain many rows and many columns, so both scroll directions are useful.
y_scroll=ttk.Scrollbar(
table_frame,
orient='vertical',
command=tree.yview
)
x_scroll=ttk.Scrollbar(
table_frame,
orient='horizontal',
command=tree.xview
)
tree.configure(
yscrollcommand=y_scroll.set,
xscrollcommand=x_scroll.set
)
The vertical scrollbar handles additional rows while the horizontal scrollbar is useful when the DataFrame contains several columns.
The complete program lets the user select different CSV files without recreating the Treeview widget. The same Treeview is cleared and reconfigured whenever another file is loaded.
The earlier version created a new Treeview inside trv_refresh() every time a CSV file was selected.
That can leave old widgets in the interface when several files are loaded.
The revised structure is:
Create Treeview once
|
v
Select CSV
|
+--> delete old rows
+--> change columns
+--> insert new rows
This is cleaner and makes repeated file selection easier to manage.
This beginner example reads the complete CSV file into memory and displays every DataFrame row.
For very large files, a later version can use:
read_csv() with chunksize;For normal learning datasets, loading the complete DataFrame keeps the example easier to understand.
This page completes the first stage of our Tkinter + Pandas workflow:
CSV
|
v
Pandas DataFrame
|
v
Treeview
The next project continues from the same DataFrame and stores its data in SQLite:
CSV
|
v
Pandas DataFrame
|
v
SQLite database
Store Selected CSV Data in SQLite using Pandas
You can also search a DataFrame and display matching records in Treeview after the data has been loaded.
The program uses filedialog.askopenfilename() to open a file browser and return the path selected by the user.
The selected path is passed to pd.read_csv(), which reads the CSV data and creates a DataFrame.
Use df.shape[0] for the number of rows and df.shape[1] for the number of columns.
The first column may contain duplicate values. Letting Treeview generate its own item identifiers avoids duplicate iid errors.
Yes. The program creates safe internal column IDs dynamically and uses the DataFrame column names as the visible Treeview headings.
The vertical scrollbar handles many DataFrame rows, while the horizontal scrollbar helps when the CSV contains more columns than fit inside the window.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.