[英]How to convert specific format of date to useful and readable format of date in Python Pandas?
I have DataFrame in Pandas like below:我在 Pandas 中有 DataFrame,如下所示:
DATA TYPES:数据类型:
ID - numeric ID - 数字
HOLIDAY - object假期 - object
YEAR - object年 - object
ID ![]() |
HOLIDAY![]() |
YEAR![]() |
---|---|---|
111 ![]() |
1 sty ![]() |
2022 ![]() |
222 ![]() |
20 kwi ![]() |
2022 ![]() |
333 ![]() |
8 mar ![]() |
2022 ![]() |
... ![]() |
... ![]() |
... ![]() |
sty - January麦粒肿 - 一月
kwi - APril奎 - 四月
mar - March三月至三月
And I need to convert above table so as to have full and useful date (as string format).我需要转换上表以获得完整且有用的日期(字符串格式)。
So, I need to have something like below:所以,我需要像下面这样的东西:
ID | HOLIDAY | YEAR
----|-------------|-------
111 | 01-01-2022 | 2022
222 | 20-02-2022 | 2022
333 | 08-03-2022 | 2022
... | ... | ...
How can I do that in Python Pandas?我怎样才能在 Python Pandas 中做到这一点?
I used somethink like that:我用了这样的想法:
df['HOLIDAY'] = pd.to_datetime(df['HOLIDAY'] +" "+ df['YEAR'] , format='%d %b %Y')
df['HOLIDAY'] = df['HOLIDAY'].dt.strftime('%d-%m-%Y')
but it generate error like the follow: ValueError: time data '1 sty 2022' does not match format '%d %b %Y' (match)
但它会产生如下错误:
ValueError: time data '1 sty 2022' does not match format '%d %b %Y' (match)
hello you can use this:你好,你可以使用这个:
d={'sty':'-1','kwi':'-4','mar':'-3'} #creat dict
a=df.HOLIDAY.tolist() # creat list of original holiday
for i in range(len(df)):
for word, replacement in d.items():
a[i] = a[i].replace(word, replacement)# creat a loop that replace the mount by her number
a[i] = a[i].replace(" ", "")# delete the space ex '1 sty' -> '1sty'
df.HOLIDAY=a
l=[]
for i in range(len(df)):
l.append(str(df.HOLIDAY[i])+'-'+str(df.YEAR[i]))#loop that concatenat year and holiday
df.HOLIDAY=l# replace holiday in df by new values
df.HOLIDAY=pd.to_datetime(df.HOLIDAY, format="%d-%m-%Y")#transform holiday from str to datetime
Try:尝试:
df["HOLIDAY"] = df["HOLIDAY"].apply(
lambda x: x.replace("sty", "January")
.replace("kwi", "April")
.replace("mar", "March")
)
df["HOLIDAY"] = pd.to_datetime(
df["HOLIDAY"] + " " + df["YEAR"].astype(str)
).dt.strftime("%d-%m-%Y")
print(df)
Prints:印刷:
ID HOLIDAY YEAR
0 111 01-01-2022 2022
1 222 20-04-2022 2022
2 333 08-03-2022 2022
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.