How to extract Time data from an Excel file column using Pandas?
Prerequisite: Regular Expressions in Python
In these articles, we will discuss how to extract Time data from an Excel file column using Pandas. Suppose our Excel file looks like below given image then we have to extract the Time from the Excel sheet column and store it into a new Dataframe column.
For viewing the Excel file Click Here.
- Import the required module.
- Import data from Excel file.
- Make an extra column for store extracted time.
- Set Index for searching for extracting column.
- Define the pattern of Time format (HH: MM: SS).
- Search Time and assigning to the respective column in Dataframe.
Let’s see Step-By-Step-Implementation:
Step 1: Import the required module and read data from Excel file.
Step 2: Make an extra column for storing Time data.
Step 3: Set Index for searching
Step 4: Defining the Regular expression (regex) for the time.
Regex for time HH/ MM/ SS format:
Step 5: Search Time and assigning to the respective column in Dataframe.
For searching the time using regex in a string we are using re.search() function of re library.
Note: Before running this program, make sure you have already installed xlrd library in your Python environment.