如何在此 Pandas 数据透视表中重新排序星期几?

发布于 2025-01-16 02:43:36 字数 1136 浏览 3 评论 0原文

import numpy as np 
import pandas as pd 
import matplotlib.pyplot as plt
import seaborn as sns
import datetime
    
df = pd.read_excel("Baltimore Towing Division.xlsx",sheet_name="TowingData")

df['Month'] = pd.DatetimeIndex(df['TowedDate']).strftime("%b")
df['Week day'] = pd.DatetimeIndex(df['TowedDate']).strftime("%a")


monthOrder = ['Jan', 'Feb', 'Mar', 'Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']
dayOrder = ['Mon','Tue','Wed','Thu','Fri','Sat','Sun']


Pivotdf = pd.pivot_table(df, index=['Month'],
                        values=['TowedDate'],
                        columns=['Week day'],
                        fill_value=0,
                        aggfunc='count').reindex(monthOrder,axis=0).reindex(dayOrder,axis=1)

print(df)

我在数据透视表末尾使用 .reindex 函数来重新索引月份和“工作日”列,它在结果中返回 NaN。

在 axis=1 中使用 .reindex Using .reindex in axis=1

不在数据透视表为我带来结果的日期列上执行 .reindex,但一周中的每一天都杂乱无章。我需要它们按如下顺序出现在表中:周一、周二、周三、周四、周五、周六、周日

Whitout 在 axis=1 中使用 .reindex 不使用 .reindex in axis=1

import numpy as np 
import pandas as pd 
import matplotlib.pyplot as plt
import seaborn as sns
import datetime
    
df = pd.read_excel("Baltimore Towing Division.xlsx",sheet_name="TowingData")

df['Month'] = pd.DatetimeIndex(df['TowedDate']).strftime("%b")
df['Week day'] = pd.DatetimeIndex(df['TowedDate']).strftime("%a")


monthOrder = ['Jan', 'Feb', 'Mar', 'Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']
dayOrder = ['Mon','Tue','Wed','Thu','Fri','Sat','Sun']


Pivotdf = pd.pivot_table(df, index=['Month'],
                        values=['TowedDate'],
                        columns=['Week day'],
                        fill_value=0,
                        aggfunc='count').reindex(monthOrder,axis=0).reindex(dayOrder,axis=1)

print(df)

I use the .reindex function at the end of the pivot table for reindex the Months and the columns 'Week day', it returns a NaN in the results.

Using .reindex in axis=1
Using .reindex in axis=1

Not doing the .reindex at the column of the days the Pivot table brings me the results, but with the days of the week disorganized. I need them to appear in the table in order like this: Mon, Tue, Wed, Thu, Fri, Sat, Sun

Whitout using .reindex in axis=1
Whitout using .reindex in axis=1

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

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

发布评论

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

评论(2

墨落画卷 2025-01-23 02:43:36

也许使用 loc:

# with values=['TowedDate'] -> MultiIndex
Pivotdf = pd.pivot_table(df, index=['Month'],
                        values=['TowedDate'],
                        columns=['Week day'],
                        fill_value=0,
                        aggfunc='count').loc[monthOrder, (slice(None), dayOrder)]

# OR

# with values='TowedDate' -> Index
Pivotdf = pd.pivot_table(df, index=['Month'],
                        values='TowedDate',
                        columns=['Week day'],
                        fill_value=0,
                        aggfunc='count').loc[monthOrder, dayOrder)]

输出:

>>> Pivotdf
         TowedDate                        
Week day       Mon Tue Wed Thu Fri Sat Sun
Month                                     
Jan              1   0   1   0   1   0   0
Feb              2   0   1   0   1   0   0
Mar              1   0   0   0   0   0   0
Apr              0   0   0   1   0   1   0
May              0   1   1   3   1   1   2
Jun              1   0   0   0   0   1   2
Jul              0   1   0   0   2   0   0
Aug              3   0   0   0   1   2   1
Sep              0   0   1   1   0   1   0
Oct              3   0   0   0   1   0   1
Nov              1   0   0   0   1   2   3
Dec              0   1   1   0   0   0   0

Maybe using loc:

# with values=['TowedDate'] -> MultiIndex
Pivotdf = pd.pivot_table(df, index=['Month'],
                        values=['TowedDate'],
                        columns=['Week day'],
                        fill_value=0,
                        aggfunc='count').loc[monthOrder, (slice(None), dayOrder)]

# OR

# with values='TowedDate' -> Index
Pivotdf = pd.pivot_table(df, index=['Month'],
                        values='TowedDate',
                        columns=['Week day'],
                        fill_value=0,
                        aggfunc='count').loc[monthOrder, dayOrder)]

Output:

>>> Pivotdf
         TowedDate                        
Week day       Mon Tue Wed Thu Fri Sat Sun
Month                                     
Jan              1   0   1   0   1   0   0
Feb              2   0   1   0   1   0   0
Mar              1   0   0   0   0   0   0
Apr              0   0   0   1   0   1   0
May              0   1   1   3   1   1   2
Jun              1   0   0   0   0   1   2
Jul              0   1   0   0   2   0   0
Aug              3   0   0   0   1   2   1
Sep              0   0   1   1   0   1   0
Oct              3   0   0   0   1   0   1
Nov              1   0   0   0   1   2   3
Dec              0   1   1   0   0   0   0
等你爱我 2025-01-23 02:43:36

Corralien的方法解决了这个问题。

# with values=['TowedDate'] -> MultiIndex
Pivotdf = pd.pivot_table(df, index=['Month'],
                        values=['TowedDate'],
                        columns=['Week day'],
                        fill_value=0,
                        aggfunc='count').loc[monthOrder, (slice(None), dayOrder)]

问题已解决

非常感谢所有在这个问题上回答和帮助我的会员

Corralien's method solved the problem.

# with values=['TowedDate'] -> MultiIndex
Pivotdf = pd.pivot_table(df, index=['Month'],
                        values=['TowedDate'],
                        columns=['Week day'],
                        fill_value=0,
                        aggfunc='count').loc[monthOrder, (slice(None), dayOrder)]

Problem solved

Millions of thanks to all the members who answered and assisted me in this question

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文