Viết chương trình Python để trích xuất năm, tháng và ngày từ một url

Bạn có thể trích xuất tháng và năm từ cột DateTime [ngày] trong gấu trúc theo nhiều cách. Trong bài viết này, tôi sẽ giải thích cách lấy năm và lấy tháng từ cột Datetime bằng cách sử dụng các phương thức


import pandas as pd
import numpy as np
import datetime
Dates = ["2018-08-14","2019-10-17","2020-11-14","2020-05-17","2021-09-15","2021-12-14"]
Courses =["Spark","PySpark","Hadoop","Python","Pandas","Hadoop"]
df = pd.DataFrame[{'InsertedDate': pd.to_datetime[Dates]},index=Courses]

# Use Datetime.strftime[] Method to extract month and year
df['Year'] = df['InsertedDate'].dt.strftime['%Y']
df['Month'] = df['InsertedDate'].dt.strftime['%m']
print[df]
3 và

import pandas as pd
import numpy as np
import datetime
Dates = ["2018-08-14","2019-10-17","2020-11-14","2020-05-17","2021-09-15","2021-12-14"]
Courses =["Spark","PySpark","Hadoop","Python","Pandas","Hadoop"]
df = pd.DataFrame[{'InsertedDate': pd.to_datetime[Dates]},index=Courses]

# Use Datetime.strftime[] Method to extract month and year
df['Year'] = df['InsertedDate'].dt.strftime['%Y']
df['Month'] = df['InsertedDate'].dt.strftime['%m']
print[df]
4 tương ứng

Nếu dữ liệu không ở kiểu Datetime, trước tiên bạn cần chuyển đổi nó thành Datetime bằng cách sử dụng phương thức


import pandas as pd
import numpy as np
import datetime
Dates = ["2018-08-14","2019-10-17","2020-11-14","2020-05-17","2021-09-15","2021-12-14"]
Courses =["Spark","PySpark","Hadoop","Python","Pandas","Hadoop"]
df = pd.DataFrame[{'InsertedDate': pd.to_datetime[Dates]},index=Courses]

# Use Datetime.strftime[] Method to extract month and year
df['Year'] = df['InsertedDate'].dt.strftime['%Y']
df['Month'] = df['InsertedDate'].dt.strftime['%m']
print[df]
5. Ngoài ra, tôi sẽ trích xuất năm và tháng bằng pandas. Thuộc tính DatetimeIndex năm và tháng _______ 6 phương thức

1. Ví dụ nhanh về trích xuất tháng và năm từ ngày giờ

Nếu bạn đang vội, dưới đây là một số ví dụ nhanh về cách trích xuất tháng và năm riêng biệt từ cột DataFrame DateTime của gấu trúc


# Use Datetime.strftime[] Method to extract month and year
df['Year'] = df['InsertedDate'].dt.strftime['%Y']
df['Month'] = df['InsertedDate'].dt.strftime['%m']

# Using pandas.Series.dt.year[] & pandas.Series.dt.month[] method
df['Year'] = df['InsertedDate'].dt.year 
df['Month'] = df['InsertedDate'].dt.month 

# Using pandas.DatetimeIndex[] to extract month and year
df['year'] = pd.DatetimeIndex[df['InsertedDate']].year
df['month'] = pd.DatetimeIndex[df['InsertedDate']].month

# Use datetime.to_period[] method to extract month and year
df['Month_Year'] = df['InsertedDate'].dt.to_period['M']

# Use DataFrame.apply[] with lambda function and strftime[]
df['Month_Year'] = df['InsertedDate'].apply[lambda x: x.strftime['%B-%Y']] 

# Use Pandas.to_datetime[] and datetime.strftime[] method
df['yyyy-mm'] = pd.to_datetime[df['InsertedDate']].dt.strftime['%Y-%m']

2. Pandas Trích xuất Tháng và Năm bằng Datetime. strftime[]

Phương thức


import pandas as pd
import numpy as np
import datetime
Dates = ["2018-08-14","2019-10-17","2020-11-14","2020-05-17","2021-09-15","2021-12-14"]
Courses =["Spark","PySpark","Hadoop","Python","Pandas","Hadoop"]
df = pd.DataFrame[{'InsertedDate': pd.to_datetime[Dates]},index=Courses]

# Use Datetime.strftime[] Method to extract month and year
df['Year'] = df['InsertedDate'].dt.strftime['%Y']
df['Month'] = df['InsertedDate'].dt.strftime['%m']
print[df]
6 lấy định dạng ngày giờ và trả về một chuỗi đại diện cho định dạng cụ thể. Bạn có thể sử dụng

import pandas as pd
import numpy as np
import datetime
Dates = ["2018-08-14","2019-10-17","2020-11-14","2020-05-17","2021-09-15","2021-12-14"]
Courses =["Spark","PySpark","Hadoop","Python","Pandas","Hadoop"]
df = pd.DataFrame[{'InsertedDate': pd.to_datetime[Dates]},index=Courses]

# Use Datetime.strftime[] Method to extract month and year
df['Year'] = df['InsertedDate'].dt.strftime['%Y']
df['Month'] = df['InsertedDate'].dt.strftime['%m']
print[df]
8 và

import pandas as pd
import numpy as np
import datetime
Dates = ["2018-08-14","2019-10-17","2020-11-14","2020-05-17","2021-09-15","2021-12-14"]
Courses =["Spark","PySpark","Hadoop","Python","Pandas","Hadoop"]
df = pd.DataFrame[{'InsertedDate': pd.to_datetime[Dates]},index=Courses]

# Use Datetime.strftime[] Method to extract month and year
df['Year'] = df['InsertedDate'].dt.strftime['%Y']
df['Month'] = df['InsertedDate'].dt.strftime['%m']
print[df]
9 làm mã định dạng để trích xuất năm và tháng tương ứng từ Khung dữ liệu của gấu trúc


import pandas as pd
import numpy as np
import datetime
Dates = ["2018-08-14","2019-10-17","2020-11-14","2020-05-17","2021-09-15","2021-12-14"]
Courses =["Spark","PySpark","Hadoop","Python","Pandas","Hadoop"]
df = pd.DataFrame[{'InsertedDate': pd.to_datetime[Dates]},index=Courses]

# Use Datetime.strftime[] Method to extract month and year
df['Year'] = df['InsertedDate'].dt.strftime['%Y']
df['Month'] = df['InsertedDate'].dt.strftime['%m']
print[df]

Sản lượng dưới sản lượng


        InsertedDate  Year Month
Spark     2018-08-14  2018    08
PySpark   2019-10-17  2019    10
Hadoop    2020-11-14  2020    11
Python    2020-05-17  2020    05
Pandas    2021-09-15  2021    09
Hadoop    2021-12-14  2021    12

3. Trích xuất tháng và năm bằng pandas. Loạt. đt. năm tháng[]

Bạn có thể sử dụng các phương thức


        InsertedDate  Year Month
Spark     2018-08-14  2018    08
PySpark   2019-10-17  2019    10
Hadoop    2020-11-14  2020    11
Python    2020-05-17  2020    05
Pandas    2021-09-15  2021    09
Hadoop    2021-12-14  2021    12
0 và

        InsertedDate  Year Month
Spark     2018-08-14  2018    08
PySpark   2019-10-17  2019    10
Hadoop    2020-11-14  2020    11
Python    2020-05-17  2020    05
Pandas    2021-09-15  2021    09
Hadoop    2021-12-14  2021    12
1 để lấy năm và tháng, nhưng những phương thức này trả về một đối tượng sê-ri. Gán những thứ này cho một cột để nhận DataFrame với các cột năm và tháng


# Using pandas.Series.dt.year[] & pandas.Series.dt.month[] method
df['Year'] = df['InsertedDate'].dt.year 
df['Month'] = df['InsertedDate'].dt.month 
print[df]

Sản lượng dưới sản lượng


        InsertedDate  Year  Month
Spark     2018-08-14  2018      8
PySpark   2019-10-17  2019     10
Hadoop    2020-11-14  2020     11
Python    2020-05-17  2020      5
Pandas    2021-09-15  2021      9
Hadoop    2021-12-14  2021     12

4. Sử dụng pandas DatetimeIndex[] để trích xuất tháng và năm

Ngoài ra, để trích xuất tháng và năm từ cột Ngày giờ của gấu trúc, hãy sử dụng thuộc tính


        InsertedDate  Year Month
Spark     2018-08-14  2018    08
PySpark   2019-10-17  2019    10
Hadoop    2020-11-14  2020    11
Python    2020-05-17  2020    05
Pandas    2021-09-15  2021    09
Hadoop    2021-12-14  2021    12
2 để tìm

        InsertedDate  Year Month
Spark     2018-08-14  2018    08
PySpark   2019-10-17  2019    10
Hadoop    2020-11-14  2020    11
Python    2020-05-17  2020    05
Pandas    2021-09-15  2021    09
Hadoop    2021-12-14  2021    12
3 và sử dụng thuộc tính

        InsertedDate  Year Month
Spark     2018-08-14  2018    08
PySpark   2019-10-17  2019    10
Hadoop    2020-11-14  2020    11
Python    2020-05-17  2020    05
Pandas    2021-09-15  2021    09
Hadoop    2021-12-14  2021    12
4 để tìm

        InsertedDate  Year Month
Spark     2018-08-14  2018    08
PySpark   2019-10-17  2019    10
Hadoop    2020-11-14  2020    11
Python    2020-05-17  2020    05
Pandas    2021-09-15  2021    09
Hadoop    2021-12-14  2021    12
5 có mặt trong ngày. Lưu ý rằng phương thức này lấy ngày làm đối số


# Using pandas.DatetimeIndex[] to extract month and year
df['year'] = pd.DatetimeIndex[df['InsertedDate']].year
df['month'] = pd.DatetimeIndex[df['InsertedDate']].month
print[df]

Mang lại đầu ra tương tự như trên

5. Sử dụng ngày giờ. to_period[] Phương thức trích xuất tháng và năm

Bạn cũng có thể sử dụng phương pháp


        InsertedDate  Year Month
Spark     2018-08-14  2018    08
PySpark   2019-10-17  2019    10
Hadoop    2020-11-14  2020    11
Python    2020-05-17  2020    05
Pandas    2021-09-15  2021    09
Hadoop    2021-12-14  2021    12
6.

        InsertedDate  Year Month
Spark     2018-08-14  2018    08
PySpark   2019-10-17  2019    10
Hadoop    2020-11-14  2020    11
Python    2020-05-17  2020    05
Pandas    2021-09-15  2021    09
Hadoop    2021-12-14  2021    12
7 phải ở định dạng ngày giờ


# Use datetime.to_period[] method to extract month and year
df['Month_Year'] = df['InsertedDate'].dt.to_period['M']
print[df]

Sản lượng dưới sản lượng


        InsertedDate   Month_Year
Spark     2018-08-14    2018-08
PySpark   2019-10-17    2019-10
Hadoop    2020-11-14    2020-11
Python    2020-05-17    2020-05
Pandas    2021-09-15    2021-09
Hadoop    2021-12-14    2021-12

6. Sử dụng Khung dữ liệu. áp dụng [] Với Hàm Lambda và strftime []

Hãy xem cách lấy tháng và năm bằng cách sử dụng Pandas DataFrame. áp dụng [] và hàm lambda


# Use DataFrame.apply[] with lambda function and strftime[]
df['Month_Year'] = df['InsertedDate'].apply[lambda x: x.strftime['%B-%Y']] 
print[df]

Sản lượng dưới sản lượng


        InsertedDate      Month_Year
Spark     2018-08-14     August-2018
PySpark   2019-10-17    October-2019
Hadoop    2020-11-14   November-2020
Python    2020-05-17        May-2020
Pandas    2021-09-15  September-2021
Hadoop    2021-12-14   December-2021

7. Sử dụng gấu trúc. to_datetime[] và ngày giờ. phương thức strftime[]

Để thêm cột có cặp 'năm tháng'


import pandas as pd
import numpy as np
import datetime
Dates = ["2018-08-14","2019-10-17","2020-11-14","2020-05-17","2021-09-15","2021-12-14"]
Courses =["Spark","PySpark","Hadoop","Python","Pandas","Hadoop"]
df = pd.DataFrame[{'InsertedDate': pd.to_datetime[Dates]},index=Courses]

# Use Datetime.strftime[] Method to extract month and year
df['Year'] = df['InsertedDate'].dt.strftime['%Y']
df['Month'] = df['InsertedDate'].dt.strftime['%m']
print[df]
0

Sản lượng dưới sản lượng


import pandas as pd
import numpy as np
import datetime
Dates = ["2018-08-14","2019-10-17","2020-11-14","2020-05-17","2021-09-15","2021-12-14"]
Courses =["Spark","PySpark","Hadoop","Python","Pandas","Hadoop"]
df = pd.DataFrame[{'InsertedDate': pd.to_datetime[Dates]},index=Courses]

# Use Datetime.strftime[] Method to extract month and year
df['Year'] = df['InsertedDate'].dt.strftime['%Y']
df['Month'] = df['InsertedDate'].dt.strftime['%m']
print[df]
1

8. Hoàn thành ví dụ để lấy tháng và năm từ Pandas Datetime


import pandas as pd
import numpy as np
import datetime
Dates = ["2018-08-14","2019-10-17","2020-11-14","2020-05-17","2021-09-15","2021-12-14"]
Courses =["Spark","PySpark","Hadoop","Python","Pandas","Hadoop"]
df = pd.DataFrame[{'InsertedDate': pd.to_datetime[Dates]},index=Courses]

# Use Datetime.strftime[] Method to extract month and year
df['Year'] = df['InsertedDate'].dt.strftime['%Y']
df['Month'] = df['InsertedDate'].dt.strftime['%m']
print[df]
2

Phần kết luận

Trong bài viết này, bạn đã học cách trích xuất tháng và năm riêng biệt từ cột Ngày giờ của pandas bằng cách sử dụng các phương thức


        InsertedDate  Year Month
Spark     2018-08-14  2018    08
PySpark   2019-10-17  2019    10
Hadoop    2020-11-14  2020    11
Python    2020-05-17  2020    05
Pandas    2021-09-15  2021    09
Hadoop    2021-12-14  2021    12
8,

        InsertedDate  Year Month
Spark     2018-08-14  2018    08
PySpark   2019-10-17  2019    10
Hadoop    2020-11-14  2020    11
Python    2020-05-17  2020    05
Pandas    2021-09-15  2021    09
Hadoop    2021-12-14  2021    12
9,

# Using pandas.Series.dt.year[] & pandas.Series.dt.month[] method
df['Year'] = df['InsertedDate'].dt.year 
df['Month'] = df['InsertedDate'].dt.month 
print[df]
0 và

# Using pandas.Series.dt.year[] & pandas.Series.dt.month[] method
df['Year'] = df['InsertedDate'].dt.year 
df['Month'] = df['InsertedDate'].dt.month 
print[df]
1 với các ví dụ

Làm cách nào để trích xuất ngày từ URL trong Python?

Bạn có thể sử dụng biểu thức chính quy [\d{4}-\d{2}-\d{2}] , điều đó có nghĩa là . Lưu câu trả lời này. Hiển thị hoạt động trên bài đăng này. Điều này sẽ trả về danh sách 2 phần tử chứa cả hai ngày và là một dòng Để vừa đến, bạn có thể thay đổi [1. ] để phù hợp với nhu cầu của bạn.

Làm cách nào để trích xuất ngày từ chuỗi Python?

Để trích xuất một ngày từ một chuỗi, trước tiên bạn phải hiểu định dạng của ngày đó. Để trích xuất ngày, chỉ cần sử dụng biểu thức chính quy và "datetime. ngày giờ. strptime" để phân tích cú pháp .

Làm cách nào để trích xuất tất cả các biến thể ngày từ dữ liệu trong thế giới thực bằng Python?

Trích xuất tất cả các biến thể ngày bằng biểu thức chính quy python [ Regex]. 3. Sắp xếp các ngày theo thứ tự thời gian tăng dần theo một số quy tắc đã nêu. .
Đang tải dữ liệu
Khai thác ngày. tôi. .
Định dạng lại ngày trích xuất. .
Chuyển đổi ngày tháng bằng chữ sang số. .
Tóm lược

Làm cách nào để tìm ngày bằng regex trong Python?

Regex ngày đơn giản [ DD/MM/YYYY] . Tuy nhiên, điều này không đảm bảo rằng ngày sẽ hợp lệ. Bạn cũng có thể thay thế \\/ bằng dấu phân cách bạn cần. kiểm tra nó.

Chủ Đề