如何在python/ colab的循环中添加一个月开始的日期?

发布于 2025-02-06 11:47:39 字数 3484 浏览 1 评论 0原文

我需要通过桌子创建您的建议。

我有一个看起来像这样的桌子。

总支出
1月至18日DOJ625000
625000DOJ 975000 1月至18日DOJ650000 JAN-18 JAN-18 DOJ 625000
975000DOJJAN
DOJ975000-
DOJJAN650000
65000018DOJ
18DOJ-
DOJ625000DOJ
18625000JAN
- -18DOJ625000
JAN-18975000DOJ
DOJDOJ650000
DOJFEBDOJ 975000 JAN-18 DOJ 650000 2月至18日DOJ 625000 DOJ 625000 FEB-18 DOJ 975000
650000650000DOJ
18625000DOJ
DOJ-18
625000975000 DOJ650000
Feb-DOJ 975000 DOJ 6500 doj975000
Feb-18doj 620000 doj625000
Feb -18DOJ975000
FEB-18DOJ650000

,这一直持续到DOJ和其他12个部门的5月22日。

我不能旋转这张表,因为在丢失日期的第一列中重复了 Tableau将增加数字,Python会丢下错误,例如

ValueError: Index contains duplicate entries, cannot reshape

每个月只有12个条目(每月12天)。我想在每个月开始时添加数字1到12,以便条目具有一个唯一的ID,可以用于df1.pivot(index ='note',columns ='dmot剂',values ='总计支出估计')

新表格应像

总支出
1-1月1日DOJ625000
2-JAN-18DOJ 975000 DOJ975000
3-JAN-18-JAN-18DOJ650000
4-JAN-JAN-18-JAN-18DOJ625000
5-JAN- JAN-- JAN-- JAN-- JAN-- 18DOJ975000
6-JAN-18DOJ650000
7-JAN-18DOJ625000
8-JAN-18DOJ975000
9-JAN-18DOJ650000 DOJ 650000
10-JAN-18625000DOJ
DOJ62500011
-JAN-JAN-18DOJ650000
1-FEB-18DOJ625000
2-FEB-18DOJ975000
-18DOJ650000
-FEB-18DOJ625000
5-FEB-18DOJ975000
FEB-1863- FEB
-4625000
8-FEB-18DOJ975000
9-FEB-18DOJ650000
10 -FEB-18 DOJ650000
11-FEB-18DOJ650000
12-FEB-18DOJ650000

,我将感谢您的建议。请帮助我,并建议如何修复此专栏。先感谢您。

I need your kind suggestions with a table creation.

I have a table that looks like this.

MonthDepartmentTotal Expenditure
Jan-18DOJ625000
Jan-18DOJ975000
Jan-18DOJ650000
Jan-18DOJ625000
Jan-18DOJ975000
Jan-18DOJ650000
Jan-18DOJ625000
Jan-18DOJ975000
Jan-18DOJ650000
Jan-18DOJ625000
Jan-18DOJ975000
Jan-18DOJ650000
Feb-18DOJ625000
Feb-18DOJ975000
Feb-18DOJ650000
Feb-18DOJ625000
Feb-18DOJ975000
Feb-18DOJ650000
Feb-18DOJ625000
Feb-18DOJ975000
Feb-18DOJ650000

And this goes on till May-22 for DOJ and 12 other departments.

I cannot pivot this table, because having duplicates in the first column where the date is missing
Tableau will add the numbers up, and Python will throw an error such as

ValueError: Index contains duplicate entries, cannot reshape

Every month has 12 entries only (12 days a month). I want to add numbers 1 to 12 at the beginning of each Month column so the entries have a unique ID that i can use for df1.pivot(index='Month',columns='Department', values='Total Expenditure Estimate')

The new table should look like

MonthDepartmentTotal Expenditure
1-Jan-18DOJ625000
2-Jan-18DOJ975000
3-Jan-18DOJ650000
4-Jan-18DOJ625000
5-Jan-18DOJ975000
6-Jan-18DOJ650000
7-Jan-18DOJ625000
8-Jan-18DOJ975000
9-Jan-18DOJ650000
10-Jan-18DOJ625000
11-Jan-18DOJ975000
12-Jan-18DOJ650000
1-Feb-18DOJ625000
2-Feb-18DOJ975000
3-Feb-18DOJ650000
4-Feb-18DOJ625000
5-Feb-18DOJ975000
6-Feb-18DOJ650000
7-Feb-18DOJ625000
8-Feb-18DOJ975000
9-Feb-18DOJ650000
10-Feb-18DOJ650000
11-Feb-18DOJ650000
12-Feb-18DOJ650000

I will appreciate your kind suggestions. Please kindly help me out and advise how to fix this column. Thank you in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

殊姿 2025-02-13 11:47:39

尝试:

df["Month"] = (
    (df.groupby(["Month", "Department"])["Month"].cumcount() + 1).astype(str)
    + "-"
    + df["Month"]
)

打印:

        Month Department  Total Expenditure
0    1-Jan-18        DOJ             625000
1    2-Jan-18        DOJ             975000
2    3-Jan-18        DOJ             650000
3    4-Jan-18        DOJ             625000
4    5-Jan-18        DOJ             975000
5    6-Jan-18        DOJ             650000
6    7-Jan-18        DOJ             625000
7    8-Jan-18        DOJ             975000
8    9-Jan-18        DOJ             650000
9   10-Jan-18        DOJ             625000
10  11-Jan-18        DOJ             975000
11  12-Jan-18        DOJ             650000
12   1-Feb-18        DOJ             625000
13   2-Feb-18        DOJ             975000
14   3-Feb-18        DOJ             650000
15   4-Feb-18        DOJ             625000
16   5-Feb-18        DOJ             975000
17   6-Feb-18        DOJ             650000
18   7-Feb-18        DOJ             625000
19   8-Feb-18        DOJ             975000
20   9-Feb-18        DOJ             650000

Try:

df["Month"] = (
    (df.groupby(["Month", "Department"])["Month"].cumcount() + 1).astype(str)
    + "-"
    + df["Month"]
)

Prints:

        Month Department  Total Expenditure
0    1-Jan-18        DOJ             625000
1    2-Jan-18        DOJ             975000
2    3-Jan-18        DOJ             650000
3    4-Jan-18        DOJ             625000
4    5-Jan-18        DOJ             975000
5    6-Jan-18        DOJ             650000
6    7-Jan-18        DOJ             625000
7    8-Jan-18        DOJ             975000
8    9-Jan-18        DOJ             650000
9   10-Jan-18        DOJ             625000
10  11-Jan-18        DOJ             975000
11  12-Jan-18        DOJ             650000
12   1-Feb-18        DOJ             625000
13   2-Feb-18        DOJ             975000
14   3-Feb-18        DOJ             650000
15   4-Feb-18        DOJ             625000
16   5-Feb-18        DOJ             975000
17   6-Feb-18        DOJ             650000
18   7-Feb-18        DOJ             625000
19   8-Feb-18        DOJ             975000
20   9-Feb-18        DOJ             650000
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文