How to create a table with clickable hyperlink to a local file in Pandas?
Last Updated :
15 Mar, 2021
If you are familiar with Data Science or Machine Learning field then we can definitely say that we are going to learn something new from this article, So let's begin. Here in this article, we will learn how to create a clickable hyperlink of the local file path using pandas. It is not a very complicated task, we can easily do it in only 4 to 5 steps. Let's dig deeper into it and talk about required libraries.
- Pandas: It is one of the most favorite and popular libraries in python for data wrangling and analysis. Pandas is the backbone of all Data Science and Machine Learning projects. It is one of the most important tools for Data Scientists and Analysts. Some features of pandas are: Data Handling, Alignment and indexing, Handling missing data, Cleaning data, Merging and joining of datasets, and much more. Here as well we will use pandas to create a dataset.
- OS library: The OS module present in python also plays a very important role in various projects. It provides lots of functions for interacting with the operating system. It includes many functions to interact with the file system as well. Here we have to work with local files that's why we need an os module to handle them.
Steps Needed
Step 1: Import required libraries.
Python3
# Importing required libraries
import pandas as pd
import os as o
Step 2: Creating a dataset using a dictionary and list.
Python3
# Creating dataset of location of files.
dataset = [dict(Images = 'img1', location = r'New/gfg.png'),
dict(Images = 'img2', location = r'New/1.png'),
dict(Images = 'img3', location = r'New/gfg2.png'),
dict(Images = 'img4', location = r'New/oled.png'),
dict(Images = 'img5', location = r'New/oled2.png')]
Here in the above code snippet, we are creating a small dataset of the local file's paths present in our local system. First, we are creating a dictionary where the image name acts as a unique key and the path of an image is acts as the value of a unique key here. Finally, converting the dictionary into a list so further we can easily convert the list into pandas dataframe.
Step 3: Converting list into Dataframe and printing it.
Python3
# Converting list into pandas dataframe
df = pd.DataFrame(dataset)
# printing the dataframe
df
Output :Â
Till Step 2 our dataframe was in list form so in Step 3 we are converting it into pandas dataframe so we can easily apply dozens of operations on it for better analysis. Currently, the path of images is not in clickable form, we have to perform some tricky operation on it to convert in into clickable hyperlink form.
Step 4: Creating a function to convert the path into Clickable form.
Python3
# Function to convert file path into clickable form.
def fun(path):
# returns the final component of a url
f_url = o.path.basename(path)
# convert the url into link
return '<a href="{}">{}</a>'.format(path, f_url)
In the above code snippet, we have created a function that will convert the file path(location of file) into a clickable hyperlink form.
Step 5: Applying the function on column "location" which will convert path into clickable hyperlink form. Now, whenever we will print the dataframe we will get our required to be output. Â Â Â Â Â
Python3
# applying function "fun" on column "location".
df = df.style.format({'location' : fun})
Output:Â
Finally, we have successfully converted our local file path into a clickable hyperlink form. Let's check out some examples.
Example 1: Creating a Dataframe that contains the clickable hyperlink path of images present in our local system.
Python3
# Step 1 : Importing required modules
import pandas as pd
import os
# Step 2 : Creating dataset of local path images
dataset = [dict(Images='img1', location=r'New/gfg.png'),
dict(Images='img2', location=r'New/1.png'),
dict(Images='img3', location=r'New/gfg2.png'),
dict(Images='img4', location=r'New/oled.png'),
dict(Images='img5', location=r'New/oled2.png')]
# Step 3 : Converting list into dataframe
df = pd.DataFrame(dataset)
# Function to convert file path into clickable form.
def fun(path):
# returns the final component of a path
f_url = os.path.basename(path)
# convert the path into clickable link
return '<a href="{}">{}</a>'.format(path, f_url)
# applying function "fun" on column "location".
df = df.style.format({'location': fun})
# Step 5 : Finally printing Dataframe
df
Output :
Example 2: Creating a Dataframe containing a clickable hyperlink path of files present in our local system.Â
Python3
# Step 1 : Importing required modules
import pandas as pd
import os
# Step 2 : Creating dataset of files
dataset = [dict(
file_names='Crop File', location=r'ML/Crop File prep.ipynb'),
dict(
file_names='Final Dataset', location=r'ML/FinalData csv creation.ipynb'),
dict(
file_names='EDA', location=r'ML/EDA on FinalData csv.ipynb'),
dict(
file_names='Model Training', location=r'ML/Model Training.ipynb'),
dict(
file_names='Yield Prediction', location=r'ML/yield prediction.ipynb')]
# Step 3 : Converting list into dataframe
df = pd.DataFrame(data)
# Function to convert file path into
# clickable hyperlink form.
def fun(path):
# returns the final component of a path
f_url = os.path.basename(path)
# convert the path into clickable hyperlink
return '<a href="{}">{}</a>'.format(path, f_url)
# Step 4 : applying make_clikable function
# on column path.
df = df.style.format({'location': fun})
# Step 5 : Finally printing Dataframe
df
Output:Â
Note: Whenever we are trying to load local files from the browser we are continuously getting an error 'Not allowed to load local resources' in our browser. If we Google this problem and came to know that it is happening due to some security issue of our system. But there is a way we can solve this problem without disturbing our system security, put the required files in the same working directory in which we are working. In the above examples, we created a folder and put all required files in it, and finally easily load them without any issue. In Example 1 we created a folder named "New" in my working directory and put all the required files in it similarly in Example 2 as well.
Similar Reads
How to Create a Hyperlink Component in MATLAB?
MATLAB is a matrix-based computational environment that has its own programming language which is very easy to use and learn. It is used for heavy mathematical concepts, understanding huge data sets with the help of the GUI Graphical User Interface of MATLAB. Â In GUIs, hyperlinked text labels are f
4 min read
How to read a CSV file to a Dataframe with custom delimiter in Pandas?
Python is a good language for doing data analysis because of the amazing ecosystem of data-centric python packages. pandas package is one of them and makes importing and analyzing data so much easier.Here, we will discuss how to load a csv file into a Dataframe. It is done using a pandas.read_csv()
3 min read
How to load a TSV file into a Pandas DataFrame?
In this article, we will discuss how to load a TSV file into a Pandas Dataframe. The idea is extremely simple we only have to first import all the required libraries and then load the data set by using various methods in Python. Dataset Used:  data.tsv Using read_csv() to load a TSV file into a Pan
1 min read
How to Filter and save the data as new files in Excel with Python Pandas
Sometimes you will want to filter and save the data as new files in Excel with Python Pandas as it can help you in selective data analysis, data organization, data sharing, etc. In this tutorial, we will learn how to filter and save the data as new files in Excel with Python Pandas. This easy guide
3 min read
How To Make Whole Row in a Table Clickable as Link in Bootstrap?
Making a table row clickable as a link means that clicking anywhere in that row takes you to a specified webpage, making navigation easier and more interactive for users. To make a whole row clickable as a link in Bootstrap, wrap each <tr> in an <a> tag or use the Bootstrap styling. Usin
2 min read
How to import an excel file into Python using Pandas?
It is not always possible to get the dataset in CSV format. So, Pandas provides us the functions to convert datasets in other formats to the Data frame. An excel file has a '.xlsx' format. Before we get started,  we need to install a few libraries. pip install pandas pip install xlrd  For importin
2 min read
How to create links with 'target="_blank"' in Markdown ?
In this article, we will see how to create links with 'target="_blank"' in Markdown. Markdown is a free and open-source lightweight markup language that can be utilized to include formatting elements to plaintext text documents. It is a special way to write words that makes them look different on a
4 min read
How to Access a Column in a DataFrame with Pandas
In this article we will explore various techniques to access a column in a dataframe with pandas with concise explanations and practical examples. Method 1: Accessing a Single Column Using Bracket NotationBracket notation is the most straightforward method to access a column. Use the syntax df['colu
4 min read
How to Create a Pivot table with multiple indexes from an excel sheet using Pandas in Python?
The term Pivot Table can be defined as the Pandas function used to create a spreadsheet-style pivot table as a DataFrame. It can be created using the pivot_table() method. Syntax: pandas.pivot_table(data, index=None) Parameters: data : DataFrame index: column, Grouper, array, or list of the previous
2 min read
How to read all CSV files in a folder in Pandas?
Our task is to read all CSV files in a folder into single Pandas dataframe. The task can be performed by first finding all CSV files in a particular folder using glob() method and then reading the file by using pandas.read_csv() method and then displaying the content. ApproachImport Required Librari
2 min read