如何从 pandas 的日期时间列中提取月份和年份-ag捕鱼王app官网

如何从 pandas 的日期时间列中提取月份和年份

作者:迹忆客 最近更新:2024/04/23 浏览次数:

我们可以分别使用 pandas.series.dt.year()pandas.series.dt.month() 方法从 datetime 列中提取年份和月份。如果数据不是 datetime 类型,则需要先将其转换为 datetime。我们还可以使用 pandas.datetimeindex.monthpandas.datetimeindex.yearstrftime() 方法提取年份和月份。


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.monthpandas.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 read_csv()函数

发布时间:2024/04/24 浏览次数:254 分类:python

pandas read_csv()函数将指定的逗号分隔值(csv)文件读取到 dataframe 中。

pandas 追加数据到 csv 中

发布时间:2024/04/24 浏览次数:352 分类:python

本教程演示了如何在追加模式下使用 to_csv()向现有的 csv 文件添加数据。

pandas 多列合并

发布时间:2024/04/24 浏览次数:628 分类:python

本教程介绍了如何在 pandas 中使用 dataframe.merge()方法合并两个 dataframes。

pandas loc vs iloc

发布时间:2024/04/24 浏览次数:837 分类:python

本教程介绍了如何使用 python 中的 loc 和 iloc 从 pandas dataframe 中过滤数据。

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便
网站地图