合并 Pandas DataFrame DateTime 列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18944993/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 21:10:55  来源:igfitidea点击:

Combine Pandas DataFrame DateTime Columns

pythonpandasdataframe

提问by Santiago Munez

Supposely I have dataframes as below:

假设我有如下数据框:

Year Month Day
2003 1     8
2003 2     7

How to combine the Year, Month, and Day in the newly defined column in the dataframe as such the dataframe would be:

如何在数据框中新定义的列中组合年、月和日,这样数据框将是:

Year Month Day Date
2003 1     8   2003-1-8
2003 2     7   2003-2-7

Any idea on this?

对此有什么想法吗?

I am using pandas python dataframe

我正在使用 Pandas python 数据框

Thanks!

谢谢!

回答by Viktor Kerkez

>>> from datetime import datetime
>>> df['Date'] = df.apply(lambda row: datetime(
                              row['Year'], row['Month'], row['Day']), axis=1)
>>> df
   Year  Month  Day                Date
0  2003      1    8 2003-01-08 00:00:00
1  2003      2    7 2003-02-07 00:00:00

Update 2020-03-12:The answer from sacul is better and faster:

2020-03-12 更新:来自 sacul 的答案更好更快:

%%timeit
df.apply(lambda row: datetime(
                              row['Year'], row['Month'], row['Day']), axis=1)

2.53 s ± 169 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

# use below, above is slow!!!
%%timeit
pd.to_datetime(df[['Year','Month','Day']])

14.4 ms ± 3.37 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)

回答by sacuL

Better use pd.to_datetime:

更好地使用pd.to_datetime

df['Date'] = pd.to_datetime(df[['Year','Month','Day']])
>>> df
   Year  Month  Day       Date
0  2003      1    8 2003-01-08
1  2003      2    7 2003-02-07