我正在使用pandas自动清理许多CSV文件。 数据看起来像这样
date value
1 13 Sep 9
2 5 Oct 8
3 10 Oct 99
我使用以下代码将字符串转换为datetime
pd.to_datetime(new_df[0].str.replace(' ', ''), format='%d%b')
(替换是为了删除有时会发生的额外空格)
这种方法很好 - 除了日期没有提供年份,大熊猫默认为1900年
date value
1 1900-09-13 9
2 1900-10-05 8
3 1900-10-10 99
幸运的是,我可以从文件名中获取年份并将其作为变量'fileyear'。但我不确定如何使用它来设置正确的日期。
我尝试过类似的事情:
pd.to_datetime(new_df[0].str.replace(' ', ''), format='%d%b' + '2016')
但这不起作用。有谁知道如何为datetimeformat提供(部分)已知日期?
答案 0 :(得分:3)
您需要更新日期格式以包含年份%d%b%Y
:
pd.to_datetime(df['date'].str.replace(' ', '') + '2016', format='%d%b%Y')
1 2016-09-13
2 2016-10-05
3 2016-10-10
Name: date, dtype: datetime64[ns]
答案 1 :(得分:2)
使用正则表达式删除两个数字之间的空格,然后使用<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.3/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.3/css/bootstrap.min.css" rel="stylesheet" />
<div class="row center-fix">
<div class="col-4 col-md-2 spacing-img">
<a href="#tab-jan" data-toggle="modal" data-target="#exampleModal">
<img src="https://afifadayu.github.io/throwback2017/asset/img/1.jpg" class="img-fluid rounded-circle" alt="jan">
</a>
</div>
<div class="col-4 col-md-2 spacing-img">
<a href="#tab-feb" data-toggle="modal" data-target="#exampleModal">
<img src="https://afifadayu.github.io/throwback2017/asset/img/2.jpg" class="img-fluid rounded-circle" alt="feb">
</a>
</div>
<div class="col-4 col-md-2 spacing-img">
<a href="#tab-mar" data-toggle="modal" data-target="#exampleModal">
<img src="https://afifadayu.github.io/throwback2017/asset/img/3.jpg" class="img-fluid rounded-circle" alt="mar">
</a>
</div>
<div class="col-4 col-md-2 spacing-img">
<a href="#tab-apr" data-toggle="modal" data-target="#exampleModal">
<img src="https://afifadayu.github.io/throwback2017/asset/img/4.jpg" class="img-fluid rounded-circle" alt="apr">
</a>
</div>
<div class="col-4 col-md-2 spacing-img">
<a href="#tab-may" data-toggle="modal" data-target="#exampleModal">
<img src="https://afifadayu.github.io/throwback2017/asset/img/5.jpg" class="img-fluid rounded-circle" alt="may">
</a>
</div>
<div class="col-4 col-md-2 spacing-img">
<a href="#tab-jun" data-toggle="modal" data-target="#exampleModal">
<img src="https://afifadayu.github.io/throwback2017/asset/img/6.jpg" class="img-fluid rounded-circle" alt="jun">
</a>
</div>
<div class="col-4 col-md-2 spacing-img">
<a href="#tab-jul" data-toggle="modal" data-target="#exampleModal">
<img src="https://afifadayu.github.io/throwback2017/asset/img/7.jpg" class="img-fluid rounded-circle" alt="jul">
</a>
</div>
<div class="col-4 col-md-2 spacing-img">
<a href="#tab-aug" data-toggle="modal" data-target="#exampleModal">
<img src="https://afifadayu.github.io/throwback2017/asset/img/8.jpg" class="img-fluid rounded-circle" alt="aug">
</a>
</div>
<div class="col-4 col-md-2 spacing-img">
<a href="#tab-sep" data-toggle="modal" data-target="#exampleModal">
<img src="https://afifadayu.github.io/throwback2017/asset/img/9.jpg" class="img-fluid rounded-circle" alt="sep">
</a>
</div>
<div class="col-4 col-md-2 spacing-img">
<a href="#tab-oct" data-toggle="modal" data-target="#exampleModal">
<img src="https://afifadayu.github.io/throwback2017/asset/img/10.jpg" class="img-fluid rounded-circle" alt="oct">
</a>
</div>
<div class="col-4 col-md-2 spacing-img">
<a href="#tab-nov" data-toggle="modal" data-target="#exampleModal">
<img src="https://afifadayu.github.io/throwback2017/asset/img/11.jpg" class="img-fluid rounded-circle" alt="nov">
</a>
</div>
<div class="col-4 col-md-2 spacing-img">
<a href="#tab-dec" data-toggle="modal" data-target="#exampleModal">
<img src="https://afifadayu.github.io/throwback2017/asset/img/12.jpg" class="img-fluid rounded-circle" alt="des">
</a>
</div>
</div>
<!-- Modal -->
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<ul class="nav nav-pills mb-3" id="pills-tab" role="tablist">
<li class="nav-item">
<a class="nav-link" data-toggle="tab" href="#tab-jan">Jan</a>
</li>
<li class="nav-item">
<a class="nav-link" data-toggle="tab" href="#tab-feb">Feb</a>
</li>
<li class="nav-item">
<a class="nav-link" id="mar-tab" data-toggle="tab" href="#tab-mar">Mar</a>
</li>
<li class="nav-item">
<a class="nav-link" id="apr-tab" data-toggle="tab" href="#tab-apr">Apr</a>
</li>
<li class="nav-item">
<a class="nav-link" id="may-tab" data-toggle="tab" href="#tab-may">May</a>
</li>
<li class="nav-item">
<a class="nav-link" id="jun-tab" data-toggle="tab" href="#tab-jun">Jun</a>
</li>
<li class="nav-item">
<a class="nav-link" id="jul-tab" data-toggle="tab" href="#tab-jul">Jul</a>
</li>
<li class="nav-item">
<a class="nav-link" id="aug-tab" data-toggle="tab" href="#tab-aug">Aug</a>
</li>
<li class="nav-item">
<a class="nav-link" id="sep-tab" data-toggle="tab" href="#tab-sep">Sep</a>
</li>
<li class="nav-item">
<a class="nav-link" id="oct-tab" data-toggle="tab" href="#tab-oct">Oct</a>
</li>
<li class="nav-item">
<a class="nav-link" id="nov-tab" data-toggle="tab" href="#tab-nov">Nov</a>
</li>
<li class="nav-item">
<a class="nav-link" id="dec-tab" data-toggle="tab" href="#tab-dec">Dec</a>
</li>
</ul>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<div class="tab-content">
<div class="tab-pane fade" id="tab-jan">
Jan
</div>
<div class="tab-pane fade" id="tab-feb">
Feb
</div>
<div class="tab-pane fade" id="tab-mar">
Mar
</div>
<div class="tab-pane fade" id="tab-apr">
Apr
</div>
<div class="tab-pane fade" id="tab-may">
May
</div>
<div class="tab-pane fade" id="tab-jun">
Jun
</div>
<div class="tab-pane fade" id="tab-jul">
Jul
</div>
<div class="tab-pane fade" id="tab-aug">
Aug
</div>
<div class="tab-pane fade" id="tab-sep">
Sep
</div>
<div class="tab-pane fade" id="tab-oct">
Oct
</div>
<div class="tab-pane fade" id="tab-nov">
Nov
</div>
<div class="tab-pane fade" id="tab-dec">
Dec
</div>
</div>
</div>
</div>
</div>
</div>
即
pd.to_datetime