使用Pandas库,构建数据框并进行透视,再使用resample方法重新采样填充缺失值,最后使用groupby方法按日期和状态进行分组计数。
示例代码如下:
import pandas as pd
data = {'date': ['2022-03-01', '2022-03-01', '2022-03-02', '2022-03-03', '2022-03-04'], 'status': ['success', 'failure', 'success', 'success', 'failure'], 'count': [10, 2, 3, 7, 5]} df = pd.DataFrame(data)
df['date'] = pd.to_datetime(df['date']) df.set_index('date', inplace=True)
pivot_table = df.pivot_table(index=pd.Grouper(freq='D'), columns='status', values='count', aggfunc='sum')
resampled_table = pivot_table.resample('D').fillna(method='ffill')
grouped_table = resampled_table.stack().reset_index(name='count').groupby(['date', 'status']).sum()
print(grouped_table)
上一篇:按日期和值筛选数组
下一篇:按日期划分的摘要日期