更改日期订单

发布于 2025-02-09 08:32:34 字数 446 浏览 2 评论 0原文

我有包含一组日期的CSV文件。

格式就像:

14/06/2000
15/08/2002
10/10/2009
09/09/2001
01/03/2003
11/12/2000
25/11/2002
23/09/2001

出于某种原因pandas.to_datementement()不起作用。 因此,我已经将列分成3列,即日,月和年。 现在,我试图将无“/”的列组合在一起:

df["period"] = df["y"].astype(str) + df["m"].astype(str)

但是问题不是得到:

 200006

我得到:

  20006

丢失了一个零。 你能帮我吗?

I have csv file containing a set of dates.

The format is like:

14/06/2000
15/08/2002
10/10/2009
09/09/2001
01/03/2003
11/12/2000
25/11/2002
23/09/2001

For some reason pandas.to_datetime() does not work on my data.
So, I have split the column into 3 columns, as day, month and year.
And now I am trying to combine the columns without "/" with:

df["period"] = df["y"].astype(str) + df["m"].astype(str)

But the problem is instead of getting:

 200006

I get:

  20006

One zero is missing.
Could you please help me with that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

惜醉颜 2025-02-16 08:32:34

这将使您可以使用日期列,然后将其变成pd.to_datetime()

#This is assuming the column name is 0 as it was on my df 
#you can change that to whatever the column name is in your dataframe
df[0] = pd.to_datetime(df[0], infer_datetime_format=True)
df[0] = df[0].sort_values(ascending = False, ignore_index = True)
df

This will allow you to take the column of dates and turn it into pd.to_datetime()

#This is assuming the column name is 0 as it was on my df 
#you can change that to whatever the column name is in your dataframe
df[0] = pd.to_datetime(df[0], infer_datetime_format=True)
df[0] = df[0].sort_values(ascending = False, ignore_index = True)
df
爱冒险 2025-02-16 08:32:34

dayfirst =参数可能会帮助您:

print(df)

            0
0  14/06/2000
1  15/08/2002
2  10/10/2009
3  09/09/2001
4  01/03/2003
5  11/12/2000
6  25/11/2002
7  23/09/2001

pd.to_datetime(df[0], dayfirst=True).sort_values()

0   2000-06-14
5   2000-12-11
3   2001-09-09
7   2001-09-23
1   2002-08-15
6   2002-11-25
4   2003-03-01
2   2009-10-10

The dayfirst= parameter might help you:

print(df)

            0
0  14/06/2000
1  15/08/2002
2  10/10/2009
3  09/09/2001
4  01/03/2003
5  11/12/2000
6  25/11/2002
7  23/09/2001

pd.to_datetime(df[0], dayfirst=True).sort_values()

0   2000-06-14
5   2000-12-11
3   2001-09-09
7   2001-09-23
1   2002-08-15
6   2002-11-25
4   2003-03-01
2   2009-10-10
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文