如何从 pandas 的日期时间列中提取月份和年份
我们可以分别使用 pandas.series.dt.year()
和 pandas.series.dt.month()
方法从 datetime 列中提取年份和月份。如果数据不是 datetime
类型,则需要先将其转换为 datetime
。我们还可以使用 pandas.datetimeindex.month
和 pandas.datetimeindex.year
和 strftime()
方法提取年份和月份。
和 pandas.series.dt.month()
方法提取月份和年份
应用于 datetime 类型的 pandas.series.dt.year()
和 pandas.series.dt.month()
方法分别返回系列对象中 datetime 条目的年和月的 numpy 数组。
import pandas as pd
import numpy as np
import datetime
list_of_dates = ["2019-11-20", "2020-01-02", "2020-02-05", "2020-03-10", "2020-04-16"]
employees = ["hisila", "shristi", "zeppy", "alina", "jerry"]
df = pd.dataframe({"joined date": pd.to_datetime(list_of_dates)}, index=employees)
df["year"] = df["joined date"].dt.year
df["month"] = df["joined date"].dt.month
print(df)
输出:
joined date year month
hisila 2019-11-20 2019 11
shristi 2020-01-02 2020 1
zeppy 2020-02-05 2020 2
alina 2020-03-10 2020 3
jerry 2020-04-16 2020 4
但是,如果该列不是 datetime
类型,则应首先使用 to_datetime()
方法将该列转换为 datetime
类型。
import pandas as pd
import numpy as np
import datetime
list_of_dates = ["11/20/2019", "01/02/2020", "02/05/2020", "03/10/2020", "04/16/2020"]
employees = ["hisila", "shristi", "zeppy", "alina", "jerry"]
df = pd.dataframe({"joined date": pd.to_datetime(list_of_dates)}, index=employees)
df["joined date"] = pd.to_datetime(df["joined date"])
df["year"] = df["joined date"].dt.year
df["month"] = df["joined date"].dt.month
print(df)
输出:
joined date year month
hisila 2019-11-20 2019 11
shristi 2020-01-02 2020 1
zeppy 2020-02-05 2020 2
alina 2020-03-10 2020 3
jerry 2020-04-16 2020 4
strftime()
方法提取年份和月份
strftime()
方法使用 datetime,将格式代码作为输入,并返回表示输出中指定的特定格式的字符串。我们使用%y
和%m
作为格式代码来提取年份和月份。
import pandas as pd
import numpy as np
import datetime
list_of_dates = ["2019-11-20", "2020-01-02", "2020-02-05", "2020-03-10", "2020-04-16"]
employees = ["hisila", "shristi", "zeppy", "alina", "jerry"]
df = pd.dataframe({"joined date": pd.to_datetime(list_of_dates)}, index=employees)
df["year"] = df["joined date"].dt.strftime("%y")
df["month"] = df["joined date"].dt.strftime("%m")
print(df)
输出:
joined date year month
hisila 2019-11-20 2019 11
shristi 2020-01-02 2020 01
zeppy 2020-02-05 2020 02
alina 2020-03-10 2020 03
jerry 2020-04-16 2020 04
pandas.datetimeindex.month
和 pandas.datetimeindex.year
提取年份和月份
从 datetime
列中提取月份和年份的另一种简单方法是检索 pandas.datetimeindex 对象的年份和月份属性的值类。
import pandas as pd
import numpy as np
import datetime
list_of_dates = ["2019-11-20", "2020-01-02", "2020-02-05", "2020-03-10", "2020-04-16"]
employees = ["hisila", "shristi", "zeppy", "alina", "jerry"]
df = pd.dataframe({"joined date": pd.to_datetime(list_of_dates)}, index=employees)
df["year"] = pd.datetimeindex(df["joined date"]).year
df["month"] = pd.datetimeindex(df["joined date"]).month
print(df)
输出:
joined date year month
hisila 2019-11-20 2019 11
shristi 2020-01-02 2020 1
zeppy 2020-02-05 2020 2
alina 2020-03-10 2020 3
jerry 2020-04-16 2020 4
pandas.datetimeindex
类是 datetime64
数据类型的不变类型 ndarray
。它具有年,月,天等属性。
转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处
本文地址:
相关文章
pandas dataframe dataframe.shift() 函数
发布时间:2024/04/24 浏览次数:133 分类:python
-
dataframe.shift() 函数是将 dataframe 的索引按指定的周期数进行移位。
python pandas.pivot_table() 函数
发布时间:2024/04/24 浏览次数:82 分类:python
-
python pandas pivot_table()函数通过对数据进行汇总,避免了数据的重复。
pandas read_csv()函数
发布时间:2024/04/24 浏览次数:254 分类:python
-
pandas read_csv()函数将指定的逗号分隔值(csv)文件读取到 dataframe 中。
pandas 多列合并
发布时间:2024/04/24 浏览次数:628 分类:python
-
本教程介绍了如何在 pandas 中使用 dataframe.merge()方法合并两个 dataframes。
pandas loc vs iloc
发布时间:2024/04/24 浏览次数:837 分类:python
-
本教程介绍了如何使用 python 中的 loc 和 iloc 从 pandas dataframe 中过滤数据。
在 python 中将 pandas 系列的日期时间转换为字符串
发布时间:2024/04/24 浏览次数:894 分类:python
-
了解如何在 python 中将 pandas 系列日期时间转换为字符串