[英]How do I create a dictionary with a date in a Pandas Pivot Table Query?
我需要創建一個 Pandas 數據透視表,它將輸出一個這樣的表:
也就是說,它將所有小於 01-Oct-20 的日期匯總為過期,然后正常地匯總所有從 01-Oct-20 開始的日期。
下面的代碼是我到目前為止所想出的。
#!/usr/bin/env python3
import pandas as pd
import numpy as np
# creating a data frame
df = pd.read_csv("CSVData2.csv")
table = pd.pivot_table(data=df,index=['Code'], columns=['Process Month'], values = ['Number'], aggfunc=sum)
print(table)
也許這對你有用?
# 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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.