首页 > 解决方案 > 将行的所有元素转换为月份和年份的名称

问题描述

我想将第 4 行(索引 3)转换为月份和年份的名称

df

我有一行包含日期的数据框。它是 datetime.datetime 对象。我想将行的所有元素转换为月份和年份的名称。

标签: pandasdatetimeapply

解决方案


选择该行,对其进行操作以提取年份和月份名称,然后替换:

datetime = pd.to_datetime(df.iloc[3])
month_year = datetime.apply(lambda x: x.month_name() + ", " + str(x.year))
df.iloc[3] = month_year

推荐阅读