Cải thiện bài viết
Lưu bài viết
Cải thiện bài viết
Lưu bài viết
ĐọcRegular Expressions in Python
Bàn luận
date_sample_data.xlsx
Điều kiện tiên quyết: Biểu cảm thường xuyên trong Python
Trong bài viết này, hãy để Lừa xem cách trích xuất ngày từ tệp Excel. Giả sử tệp Excel của chúng tôi trông giống như hình ảnh đã cho sau đó chúng tôi phải trích xuất ngày từ chuỗi và lưu trữ nó vào cột DataFrame mới.
- Để xem tệp Excel bấm vào đây.
- Cách tiếp cận :
- Nhập mô -đun yêu cầu.
- Nhập dữ liệu từ tệp Excel.
- Tạo thêm một cột cho một ngày mới.
- Đặt chỉ mục để tìm kiếm.
Xác định mẫu định dạng ngày.
Ngày tìm kiếm và gán cho cột tương ứng trong DataFrame.Import the required module and read data from the Excel file.
Python3
Hãy cùng xem các bước tiến từng bước:
Bước 1: Nhập mô -đun cần thiết và đọc dữ liệu từ tệp Excel.
import
pandas as pd;
02/04/2020 02 -----> [0 to 9] --> [0-9] number of character inside the string {2} [ i.e DD] 04- ----> [0 to 9] --> [0-9] number of character inside the string {2} [ i.e MM] 2020 -->[0 to 9] -->[0-9] number of character inside the sting {4} [ i.e YYYY]1
02/04/2020 02 -----> [0 to 9] --> [0-9] number of character inside the string {2} [ i.e DD] 04- ----> [0 to 9] --> [0-9] number of character inside the string {2} [ i.e MM] 2020 -->[0 to 9] -->[0-9] number of character inside the sting {4} [ i.e YYYY]2
02/04/2020 02 -----> [0 to 9] --> [0-9] number of character inside the string {2} [ i.e DD] 04- ----> [0 to 9] --> [0-9] number of character inside the string {2} [ i.e MM] 2020 -->[0 to 9] -->[0-9] number of character inside the sting {4} [ i.e YYYY]3
02/04/2020 02 -----> [0 to 9] --> [0-9] number of character inside the string {2} [ i.e DD] 04- ----> [0 to 9] --> [0-9] number of character inside the string {2} [ i.e MM] 2020 -->[0 to 9] -->[0-9] number of character inside the sting {4} [ i.e YYYY]4
02/04/2020 02 -----> [0 to 9] --> [0-9] number of character inside the string {2} [ i.e DD] 04- ----> [0 to 9] --> [0-9] number of character inside the string {2} [ i.e MM] 2020 -->[0 to 9] -->[0-9] number of character inside the sting {4} [ i.e YYYY]5
Output:
import
re;
Make an extra column for a new date.
Python3
data
=
pd.read_excel[
"date_sample_data.xlsx"
____10
02/04/2020 02 -----> [0 to 9] --> [0-9] number of character inside the string {2} [ i.e DD] 04- ----> [0 to 9] --> [0-9] number of character inside the string {2} [ i.e MM] 2020 -->[0 to 9] -->[0-9] number of character inside the sting {4} [ i.e YYYY]5
Output:
Bước 2: Tạo thêm một cột cho một ngày mới.Set Index for searching.
Python3
02/04/2020 02 -----> [0 to 9] --> [0-9] number of character inside the string {2} [ i.e DD] 04- ----> [0 to 9] --> [0-9] number of character inside the string {2} [ i.e MM] 2020 -->[0 to 9] -->[0-9] number of character inside the sting {4} [ i.e YYYY]6
02/04/2020 02 -----> [0 to 9] --> [0-9] number of character inside the string {2} [ i.e DD] 04- ----> [0 to 9] --> [0-9] number of character inside the string {2} [ i.e MM] 2020 -->[0 to 9] -->[0-9] number of character inside the sting {4} [ i.e YYYY]7
02/04/2020 02 -----> [0 to 9] --> [0-9] number of character inside the string {2} [ i.e DD] 04- ----> [0 to 9] --> [0-9] number of character inside the string {2} [ i.e MM] 2020 -->[0 to 9] -->[0-9] number of character inside the sting {4} [ i.e YYYY]8
=
import
0Bước 3: Đặt chỉ mục để tìm kiếm.
02/04/2020 02 -----> [0 to 9] --> [0-9] number of character inside the string {2} [ i.e DD] 04- ----> [0 to 9] --> [0-9] number of character inside the string {2} [ i.e MM] 2020 -->[0 to 9] -->[0-9] number of character inside the sting {4} [ i.e YYYY]1
pandas as pd;
3Output:
1 2
import
2=
import
4import
502/04/2020
02 -----> [0 to 9] --> [0-9]
number of character inside the string {2} [ i.e DD]
04- ----> [0 to 9] --> [0-9]
number of character inside the string {2} [ i.e MM]
2020 -->[0 to 9] -->[0-9]
number of character inside the sting {4} [ i.e YYYY]
4Defining the Pattern of the date format.
import
7=
import
4
02/04/2020 02 -----> [0 to 9] --> [0-9] number of character inside the string {2} [ i.e DD] 04- ----> [0 to 9] --> [0-9] number of character inside the string {2} [ i.e MM] 2020 -->[0 to 9] -->[0-9] number of character inside the sting {4} [ i.e YYYY]7
02/04/2020 02 -----> [0 to 9] --> [0-9] number of character inside the string {2} [ i.e DD] 04- ----> [0 to 9] --> [0-9] number of character inside the string {2} [ i.e MM] 2020 -->[0 to 9] -->[0-9] number of character inside the sting {4} [ i.e YYYY]4DD/MM/YY format. Use the [0-9] expression to find any character between the brackets that is a digit. Use escape sequence “\” for escaping “/” a special symbol and {2}, {4} is used to denote no of times a character belongs to the given string. So the expression become ‘[0-9]{2}\/[0-9]{2}\/[0-9]{4}’.
Example:
02/04/2020 02 -----> [0 to 9] --> [0-9] number of character inside the string {2} [ i.e DD] 04- ----> [0 to 9] --> [0-9] number of character inside the string {2} [ i.e MM] 2020 -->[0 to 9] -->[0-9] number of character inside the sting {4} [ i.e YYYY]
Python3
Bước 4: Xác định mẫu của định dạng ngày.
Chúng ta cần tạo một biểu thức thông thường cho mẫu ngày ở định dạng DD/mm/yy. Sử dụng biểu thức [0-9] để tìm bất kỳ ký tự nào giữa các dấu ngoặc là một chữ số. Sử dụng trình tự thoát hiểm \ \ để thoát khỏi một biểu tượng đặc biệt và {2}, {4} được sử dụng để biểu thị không có lần một ký tự nào thuộc về chuỗi đã cho. Vì vậy, biểu thức trở thành ‘[0-9] {2} \/[0-9] {2} \/[0-9] {4}.Search Date and assigning to the respective column in Dataframe.
pandas as pd;
4=
pandas as pd;
6pandas as pd;
7re.search[] function of re library.
Python3
Bước 5: Ngày tìm kiếm và gán cho cột tương ứng trong DataFrame.
Để tìm kiếm ngày sử dụng regex trong một chuỗi, chúng tôi đang sử dụng hàm re.search [] của thư viện lại.
pandas as pd;
8 pandas as pd;
9import
0 import
1
02/04/2020 02 -----> [0 to 9] --> [0-9] number of character inside the string {2} [ i.e DD] 04- ----> [0 to 9] --> [0-9] number of character inside the string {2} [ i.e MM] 2020 -->[0 to 9] -->[0-9] number of character inside the sting {4} [ i.e YYYY]2
import
3import
4import
5import
602/04/2020 02 -----> [0 to 9] --> [0-9] number of character inside the string {2} [ i.e DD] 04- ----> [0 to 9] --> [0-9] number of character inside the string {2} [ i.e MM] 2020 -->[0 to 9] -->[0-9] number of character inside the sting {4} [ i.e YYYY]5
Output:
import
7import
8=
re;
0
Python3
Hãy cùng xem các bước tiến từng bước:
Bước 1: Nhập mô -đun cần thiết và đọc dữ liệu từ tệp Excel.
import
pandas as pd;
02/04/2020 02 -----> [0 to 9] --> [0-9] number of character inside the string {2} [ i.e DD] 04- ----> [0 to 9] --> [0-9] number of character inside the string {2} [ i.e MM] 2020 -->[0 to 9] -->[0-9] number of character inside the sting {4} [ i.e YYYY]1
02/04/2020 02 -----> [0 to 9] --> [0-9] number of character inside the string {2} [ i.e DD] 04- ----> [0 to 9] --> [0-9] number of character inside the string {2} [ i.e MM] 2020 -->[0 to 9] -->[0-9] number of character inside the sting {4} [ i.e YYYY]2
data
7data
8data
9=
0
import
re;
02/04/2020 02 -----> [0 to 9] --> [0-9] number of character inside the string {2} [ i.e DD] 04- ----> [0 to 9] --> [0-9] number of character inside the string {2} [ i.e MM] 2020 -->[0 to 9] -->[0-9] number of character inside the sting {4} [ i.e YYYY]6
02/04/2020 02 -----> [0 to 9] --> [0-9] number of character inside the string {2} [ i.e DD] 04- ----> [0 to 9] --> [0-9] number of character inside the string {2} [ i.e MM] 2020 -->[0 to 9] -->[0-9] number of character inside the sting {4} [ i.e YYYY]7
02/04/2020 02 -----> [0 to 9] --> [0-9] number of character inside the string {2} [ i.e DD] 04- ----> [0 to 9] --> [0-9] number of character inside the string {2} [ i.e MM] 2020 -->[0 to 9] -->[0-9] number of character inside the sting {4} [ i.e YYYY]8
=
import
0Bước 3: Đặt chỉ mục để tìm kiếm.
02/04/2020 02 -----> [0 to 9] --> [0-9] number of character inside the string {2} [ i.e DD] 04- ----> [0 to 9] --> [0-9] number of character inside the string {2} [ i.e MM] 2020 -->[0 to 9] -->[0-9] number of character inside the sting {4} [ i.e YYYY]1
pandas as pd;
3Bước 4: Xác định mẫu của định dạng ngày.
Bước 5: Ngày tìm kiếm và gán cho cột tương ứng trong DataFrame.
Để tìm kiếm ngày sử dụng regex trong một chuỗi, chúng tôi đang sử dụng hàm re.search [] của thư viện lại.
02/04/2020 02 -----> [0 to 9] --> [0-9] number of character inside the string {2} [ i.e DD] 04- ----> [0 to 9] --> [0-9] number of character inside the string {2} [ i.e MM] 2020 -->[0 to 9] -->[0-9] number of character inside the sting {4} [ i.e YYYY]05
02/04/2020 02 -----> [0 to 9] --> [0-9] number of character inside the string {2} [ i.e DD] 04- ----> [0 to 9] --> [0-9] number of character inside the string {2} [ i.e MM] 2020 -->[0 to 9] -->[0-9] number of character inside the sting {4} [ i.e YYYY]06
pandas as pd;
8 pandas as pd;
9import
0 import
1
02/04/2020 02 -----> [0 to 9] --> [0-9] number of character inside the string {2} [ i.e DD] 04- ----> [0 to 9] --> [0-9] number of character inside the string {2} [ i.e MM] 2020 -->[0 to 9] -->[0-9] number of character inside the sting {4} [ i.e YYYY]2
import
3import
4import
5import
602/04/2020 02 -----> [0 to 9] --> [0-9] number of character inside the string {2} [ i.e DD] 04- ----> [0 to 9] --> [0-9] number of character inside the string {2} [ i.e MM] 2020 -->[0 to 9] -->[0-9] number of character inside the sting {4} [ i.e YYYY]5
Output:
import
7import
8=
re;
0 Before running this program, make sure you have already installed xlrd library in your Python environment.