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 ứngNế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ức1. 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]
0Sả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]
18. 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]
2Phầ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ụ