【发布时间】:2021-12-29 09:26:06
【问题描述】:
我有一个数据框,可以为我提供各种文章的每日数量水平。我想获得一个数据框,它可以为我提供每篇文章每个月最后一天的数量水平。
原始 df:
| item | Date | Quantity |
|---|---|---|
| apple | 23/09/21 | 2143 |
| bat | 21/09/2021 | 2444 |
| cola | 15/09/21 | 1512 |
| apple | 21/10/21 | 2906 |
| bat | 4/10/21 | 2730 |
| cola | 16/10/21 | 2449 |
| cola | 31/12/2021 | 0 |
| apple | 27/12/2021 | 1086 |
| bat | 25/12/2021 | 1186 |
| apple | 26/12/2021 | 1377 |
目标df:
| item | Date | Quantity |
|---|---|---|
| cola | 31/12/2021 | 0 |
| apple | 27/12/2021 | 1086 |
| bat | 25/12/2021 | 1186 |
有没有办法获得?
我尝试使用 tail() 按项目和日期分组,但没有成功。
【问题讨论】:
-
“我尝试使用 tail() 按项目和日期分组,但没有成功。”显示您尝试过的内容。 How to Askminimal reproducible example
-
在原始数据框中,您的日期是 9 月和 10 月,而在目标 df 中,您的日期都在 12 月。您的问题是“每个月最后一天的数量水平”文章”,请澄清这一点
标签: python pandas dataframe group-by