如何在此 Pandas 数据透视表 Table 中重新排序星期几?
How to reorder the days of week in this Pandas Pivot Table?
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)
我在数据透视 table 末尾使用 .reindex 函数重新索引月份和列 'Week day',结果 returns 为 NaN。
在 axis=1 中使用 .reindex
没有在日期列中执行 .reindex,Pivot table 给我带来了结果,但是一周中的日子杂乱无章。我需要它们按如下顺序出现在 table 中:周一、周二、周三、周四、周五、周六、周日
Whitout 在 axis=1 中使用 .reindex
也许使用 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
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)]
非常感谢在这个问题上回答和帮助我的所有成员
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)
我在数据透视 table 末尾使用 .reindex 函数重新索引月份和列 'Week day',结果 returns 为 NaN。
在 axis=1 中使用 .reindex
没有在日期列中执行 .reindex,Pivot table 给我带来了结果,但是一周中的日子杂乱无章。我需要它们按如下顺序出现在 table 中:周一、周二、周三、周四、周五、周六、周日
Whitout 在 axis=1 中使用 .reindex
也许使用 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
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)]
非常感谢在这个问题上回答和帮助我的所有成员