我從網上下載了一個df,基本上是這樣的。我只想保留每日值的行。有沒有可能這樣做,所以每次我下載 df 時,我都會得到最后一個每日值?
| 日期 | 姓名 |
|---|---|
| 21-02-2013 | 1231 |
| 22-02-2013 | 323 |
| 23-02-2013 | 343 |
| ... | ... |
| 12-10-2020 | 432 |
| 每月 | |
| 02-2013 | 2655 |
| 03-2013 | 321 |
| ... | ... |
| 每年 | |
| 2013年共計 | 3213 |
| 2020年總計 | 543 |
uj5u.com熱心網友回復:
您可以在“日期”列上執行正則運算式匹配;對于給定的示例資料
df
Out[10]:
Date name
0 21-02-2013 1231
1 22-02-2013 323
2 23-02-2013 343
3 ... ...
4 12-10-2020 432
5 Monthly NaN
6 02-2013 2655
7 03-2013 321
8 ... ...
9 Yearly NaN
10 Total 2013 3213
11 Total 2020 543
那將是
df.loc[df['Date'].str.match('[0-9]{2}\-[0-9]{2}\-[0-9]{4}')]
Out[11]:
Date name
0 21-02-2013 1231
1 22-02-2013 323
2 23-02-2013 343
4 12-10-2020 432
# or if you need it to be even more specific:
# df.loc[df['Date'].str.fullmatch('[0-9]{2}\-[0-9]{2}\-[0-9]{4}')]
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/313658.html
上一篇:按日期/時間整理資料
