[英]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.