更改数据框日期列的日期格式

时间:2018-08-13 12:32:25

标签: python python-3.x pandas dataframe

我有这个熊猫数据框<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/css/bootstrap.min.css" rel="stylesheet" /> <div class="row equal"> <div class="col-md-8"> <div> <p style="background-color:pink;">Lorem ipsum, dolor sit amet consectetur adipisicing elit. Quis quibusdam nobis praesentium hic vero quo quia error sed commodi cumque, illo porro eaque ad ipsa. Error maxime consequatur officiis alias fuga mollitia quas, magnam delectus necessitatibus illum, quos aperiam doloremque eaque explicabo architecto dolor? Placeat quos nemo natus velit aliquam?</p> </div> </div> <div class="col-md-4"> <div> <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Adipisci blanditiis commodi sapiente totam sequi consectetur provident. Voluptatibus corrupti odit reiciendis, aliquid temporibus dolores tempore optio delectus nesciunt ipsam veritatis quisquam facilis voluptatem voluptates inventore aliquam, praesentium, assumenda ducimus ratione quasi nisi sunt consequatur corporis soluta. Deleniti quam harum soluta alias minima voluptas iusto voluptates, iure necessitatibus assumenda ex. Minus numquam beatae vero aperiam cupiditate assumenda ipsam hic ad corporis placeat facere consequuntur consectetur sint, aspernatur mollitia optio odit eos officia nostrum? Totam asperiores quia sequi autem. Ex nostrum fuga, et, illo iusto inventore officiis facere minima hic sint obcaecati consectetur.</p> </div> </div> </div>

df

我想将Name Date Score Score2 Joe 26-12-2007 53.45 53.4500 Joe 27-12-2007 52.38 52.7399 Joe 28-12-2007 51.71 51.8500 列中的日期格式从Date转换为dd-mm-yyyy。转换后的数据帧将如下所示;

yyyy-mm-dd

我正在使用python v3.6

编辑:重复的问题假定原始日期格式为Name Date Score Score2 Joe 2007-12-26 53.45 53.4500 Joe 2007-12-27 52.38 52.7399 Joe 2007-12-28 51.71 51.8500 。但是,我的原始日期格式为yyyy-mm-dd。如果要在该问题中使用答案,则转换后的日期是错误的。 How to change the datetime format in pandas

2 个答案:

答案 0 :(得分:2)

我认为您需要这个:

df['Date'] = df['Date'].dt.strftime('%Y-%m-%d')

答案 1 :(得分:2)

使用:

df['Date'] = pd.to_datetime(df['Date']).dt.strftime('%Y-%m-%d')