1 回答
TA贡献1818条经验 获得超7个赞
也许这对你有用?
# Recreating your dataframe in code
df = pd.DataFrame({'Code':'A1 P2 B3 B3 C4 A1 B3 A1 A1'.split(' '),
'Branch':'UW2 RQ2 UW2 UW2 X01 X01 DN9 PE7 PE7'.split(' '),
'Process Month':'01-Oct-20 01-Nov-20 01-Sep-20 01-Sep-20 01-Aug-20 01-Oct-20 01-Sep-20 01-Dec-20 01-Sep-20'.split(' '),
'Number':[1]*9})
#Change string to datetime dtype
df['Process Month'] = pd.to_datetime(df['Process Month'])
# Create mask to defined 'Overdue'
m = df['Process Month'] < '01-Oct-20'
# Output Process Month back as string
df['Process Month'] = df['Process Month'].dt.strftime('%d-%b-%Y')
# Overwriting Process Month with 'OverDue' per mask above
df.loc[m, 'Process Month'] = 'OverDue'
# Creating a crosstab with totals
df_out = pd.crosstab(df['Code'], df['Process Month'], margins=True, margins_name='Total')
df_out.drop('Total', axis=1) #Don't need row Totals column
输出:
Process Month 01-Dec-2020 01-Nov-2020 01-Oct-2020 OverDue
Code
A1 1 0 2 1
B3 0 0 0 3
C4 0 0 0 1
P2 0 1 0 0
Total 1 1 2 5
添加回答
举报