<small id='O3xpQ'></small><noframes id='O3xpQ'>

      <bdo id='O3xpQ'></bdo><ul id='O3xpQ'></ul>

    1. <legend id='O3xpQ'><style id='O3xpQ'><dir id='O3xpQ'><q id='O3xpQ'></q></dir></style></legend>
        <tfoot id='O3xpQ'></tfoot>
        <i id='O3xpQ'><tr id='O3xpQ'><dt id='O3xpQ'><q id='O3xpQ'><span id='O3xpQ'><b id='O3xpQ'><form id='O3xpQ'><ins id='O3xpQ'></ins><ul id='O3xpQ'></ul><sub id='O3xpQ'></sub></form><legend id='O3xpQ'></legend><bdo id='O3xpQ'><pre id='O3xpQ'><center id='O3xpQ'></center></pre></bdo></b><th id='O3xpQ'></th></span></q></dt></tr></i><div id='O3xpQ'><tfoot id='O3xpQ'></tfoot><dl id='O3xpQ'><fieldset id='O3xpQ'></fieldset></dl></div>

        如何将年、月和日列合并到单个日期时间列?

        时间:2023-09-14
          <bdo id='5J0g9'></bdo><ul id='5J0g9'></ul>

          <small id='5J0g9'></small><noframes id='5J0g9'>

          <legend id='5J0g9'><style id='5J0g9'><dir id='5J0g9'><q id='5J0g9'></q></dir></style></legend>

            • <tfoot id='5J0g9'></tfoot>
              <i id='5J0g9'><tr id='5J0g9'><dt id='5J0g9'><q id='5J0g9'><span id='5J0g9'><b id='5J0g9'><form id='5J0g9'><ins id='5J0g9'></ins><ul id='5J0g9'></ul><sub id='5J0g9'></sub></form><legend id='5J0g9'></legend><bdo id='5J0g9'><pre id='5J0g9'><center id='5J0g9'></center></pre></bdo></b><th id='5J0g9'></th></span></q></dt></tr></i><div id='5J0g9'><tfoot id='5J0g9'></tfoot><dl id='5J0g9'><fieldset id='5J0g9'></fieldset></dl></div>

                <tbody id='5J0g9'></tbody>

                  本文介绍了如何将年、月和日列合并到单个日期时间列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有以下数据框df:

                          id  lat        lon      year    month   day         
                  0       381 53.30660   -0.54649 2004    1       2       
                  1       381 53.30660   -0.54649 2004    1       3            
                  2       381 53.30660   -0.54649 2004    1       4   
                  

                  我想创建一个新列 df['Date'],其中 yearmonthday 列按 yyyy-md 格式组合.

                  and I want to create a new column df['Date'] where the year, month, and day columns are combined according to the format yyyy-m-d.

                  在这篇文章之后,我做到了:

                  `df['Date']=pd.to_datetime(df['year']*10000000000
                                             +df['month']*100000000
                                             +df['day']*1000000,
                                             format='%Y-%m-%d%')`
                  

                  结果不是我预期的,因为它是从 1970 年而不是 2004 年开始的,而且它还包含我没有指定的小时戳:

                  The result is not what I expected, as it starts from 1970 instead of 2004, and it also contains the hour stamp, which I did not specify:

                          id  lat        lon      year    month   day  Date           
                  0       381 53.30660   -0.54649 2004    1       2    1970-01-01 05:34:00.102    
                  1       381 53.30660   -0.54649 2004    1       3    1970-01-01 05:34:00.103         
                  2       381 53.30660   -0.54649 2004    1       4    1970-01-01 05:34:00.104
                  

                  由于日期应该是 2004-1-2 格式,我做错了什么?

                  As the dates should be in the 2004-1-2 format, what am I doing wrong?

                  推荐答案

                  有一个更简单的方法:

                  In [250]: df['Date']=pd.to_datetime(df[['year','month','day']])
                  
                  In [251]: df
                  Out[251]:
                      id      lat      lon  year  month  day       Date
                  0  381  53.3066 -0.54649  2004      1    2 2004-01-02
                  1  381  53.3066 -0.54649  2004      1    3 2004-01-03
                  2  381  53.3066 -0.54649  2004      1    4 2004-01-04
                  

                  来自 文档:

                  从 DataFrame 的多列中组装日期时间.按键可以是常见的缩写,如 [yearmonthdayminutesecondmsusns])或相同的复数形式

                  Assembling a datetime from multiple columns of a DataFrame. The keys can be common abbreviations like [year, month, day, minute, second, ms, us, ns]) or plurals of the same

                  这篇关于如何将年、月和日列合并到单个日期时间列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Python UTC 日期时间对象的 ISO 格式不包括 Z(祖鲁语 下一篇:我怎样才能让 pandas 时间戳抵消一定的月份?

                  相关文章

                  最新文章

                  <tfoot id='ubasI'></tfoot>
                      <bdo id='ubasI'></bdo><ul id='ubasI'></ul>

                  1. <small id='ubasI'></small><noframes id='ubasI'>

                    <i id='ubasI'><tr id='ubasI'><dt id='ubasI'><q id='ubasI'><span id='ubasI'><b id='ubasI'><form id='ubasI'><ins id='ubasI'></ins><ul id='ubasI'></ul><sub id='ubasI'></sub></form><legend id='ubasI'></legend><bdo id='ubasI'><pre id='ubasI'><center id='ubasI'></center></pre></bdo></b><th id='ubasI'></th></span></q></dt></tr></i><div id='ubasI'><tfoot id='ubasI'></tfoot><dl id='ubasI'><fieldset id='ubasI'></fieldset></dl></div>

                      <legend id='ubasI'><style id='ubasI'><dir id='ubasI'><q id='ubasI'></q></dir></style></legend>