擅长:python、mysql、java
<p>这应该做到:</p>
<pre><code># Add this if "Induction Date" is not yet in pandas datetime format
# df["Induction Date"] = pd.to_datetime(df["Induction Date"])
df['Week'] = df['Induction Date'].dt.to_period('W-SAT').dt.week.astype(str).str.zfill(2)
</code></pre>
<p>通过<code>.dt.to_period('W-SAT')</code>,您可以指定一周的结束时间,在本例中是星期六</p>
<p>结果如下所示:</p>
<pre><code>+ + +
| Induction Date | Week |
| + |
| 2019-12-27 | 52 |
| 2019-12-28 | 52 |
| 2019-12-29 | 01 |
| 2019-12-30 | 01 |
| 2019-12-31 | 01 |
| 2020-01-01 | 01 |
| 2020-01-02 | 01 |
| 2020-01-03 | 01 |
| 2020-01-04 | 01 |
| 2020-01-05 | 02 |
+ + +
</code></pre>