简体   繁体   English

Python Pandas:将日期转换为文本,例如 2018-08 --> August

[英]Python Pandas: Convert Date to Text e.g. 2018-08 --> August

Forgive me if the format of this is incorrect, it's my first post.如果格式不正确,请原谅我,这是我的第一篇文章。

I'm using Python with Pandas and I have a column named "Month" with the values underneath such as "2018-08" for August 2018, obviously.我正在将 Python 与 Pandas 一起使用,并且我有一个名为“Month”的列,下面的值很明显,例如 2018 年 8 月的“2018-08”。 I'm trying to change the "2018-08" values to "Aug" or August".我正在尝试将“2018-08”值更改为“Aug”或“August”。

What I tried so far is:到目前为止我尝试过的是:

df = df.rename(index={"2018-08":"Aug"}) df = df.rename(index={"2018-08":"Aug"})

Also tried '' instead of "" in case it made a difference.还尝试了 '' 而不是 "" 以防万一。

I ran df.dtypes and I noticed columns "Month" is object if this makes any difference.我运行了 df.dtypes 并且我注意到列“Month”是对象,如果这有什么区别的话。

Can anyone lend a hand as of how to convert the numerical values to string?任何人都可以帮忙了解如何将数值转换为字符串?

Apologies again if this looks like a mess!如果这看起来一团糟,再次道歉!

regards问候

You can use datetime routines like so:您可以像这样使用datetime例程:

datetime.datetime.strptime('2018-08', "%Y-%m").strftime('%b') # 'Aug'

or或者

datetime.datetime.strptime('2018-08', "%Y-%m").strftime('%B') # 'August'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python Pandas:将日期转换为文本,例如,2018年8月-> 08-2018或01-08-2018 - Python Pandas: Convert Date to Text e.g. Aug 2018 --> 08-2018 or 01-08-2018 从给定的工作日和月份计算日期,但年份可变(例如,“x”年 8 月的第 3 个星期日) - Calculate date from given weekday and month, but variable year (e.g. 3rd Sunday in August in "x" year) 将 xlsx 数据集的日期从 YYYY.TEXT(例如 2012.916667)转换为正常日期格式(例如 01/01/2012) - Convert date from xlsx dataset from YYYY.TEXT (e.g. 2012.916667) to a normal date format (e.g. 01/01/2012) 如何将功能“附加”到 Python 中的对象,例如 Pandas DataFrame? - How to "attach" functionality to objects in Python e.g. to pandas DataFrame? python pandas dataframe 填充,例如 bfill、ffill - python pandas dataframe filling e.g. bfill, ffill 用于通过自定义代码访问 Smart Lock 系统的现有 API 或文档? (例如耶鲁大学,八月) - Existing APIs or documentation for accessing Smart Lock systems via custom code? (e.g. Yale, August) 如何在 python 中将军用时间格式(例如 1305)转换为 hh:mm(例如 13:05)? - How to convert military time format (e.g. 1305) to hh:mm (e.g. 13:05) in python? 如何在 SQL 中转换以下每周日期格式:(例如 2015-W34) - How do I convert the following weekly Date format in SQL: (e.g. 2015-W34) 如何将出生年份的熊猫数据框列转换为年龄? (例如'1991'-> 28) - How can I convert a pandas dataframe column with birth year to age? (e.g. '1991' -> 28) 如何将多个 pandas 数据帧(例如二维矩阵)转换为张量? - How to convert multiple pandas data-frame (e.g. 2D matrices) into a tensor?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM