我正在嘗試將零附加到任何特定日期都不存在的產品 ID 資料中,并且我的代碼需要大量時間來附加零。在 pandas/numpy 中尋找替代方法。
這是示例資料:
rpt_date product_id total_views total_cart_adds total_order_unit total_gmv
30-07-2022 mp000000006243574 7 1 0 0
30-07-2022 mp000000006292285 1 0 0 0
30-07-2022 mp000000006294016 18 1 0 0
31-07-2022 mp000000006243574 8 2 0 0
31-07-2022 mp000000006292285 5 0 0 0
例如,如果產品 id 'mp000000006294016' 的資料在 31 日或任何其他日期不存在,那么它應該為相應的列附加 0
下面是我的代碼
df_ans=pd.DataFrame()
for x in prod_df['product_id']:
df2=pd.DataFrame()
print(prod_df[prod_df['product_id']==x])
df2 = prod_df[prod_df['product_id']==x]
if df2.shape[0] == 1:
formatted_date1 = time.strptime(prod_df['rpt_date'][0], "%d-%m-%Y")
formatted_date2 = time.strptime('30-07-2022', "%d-%m-%Y")
if formatted_date1==formatted_date2:
df2.loc[-1] = ['31-07-2022', x, '0', '0','0','0'] # adding a row
df2.index = df2.index 1 # shifting index
df2 = df2.sort_index()
else:
df2.loc[-1] = ['30-07-2022', x, '0', '0','0','0'] # adding a row
df2.index = df2.index 1 # shifting index
df2 = df2.sort_index()
print(df2)
df_ans= pd.concat([df_ans,df2])
print("***************************************")
uj5u.com熱心網友回復:
您幾乎肯定應該在生成資料的查詢中解決這個問題。但是解決它的一種方法是創建一個包含所有rpt_date和product_id組合的“空白”資料幀,然后concat將其添加到原始資料并洗掉所有重復的行:
df_prid = prod_df[['product_id']].drop_duplicates()
df_date = prod_df[['rpt_date']].drop_duplicates()
df_blank = df_date.merge(df_prid, how='cross')
df_blank[['total_views', 'total_cart_adds', 'total_order_unit', 'total_gmv']] = [0,0,0,0]
df_final = pd.concat([prod_df, df_blank]).drop_duplicates(subset=['rpt_date', 'product_id'], keep='first').reset_index()
輸出:
index rpt_date product_id total_views total_cart_adds total_order_unit total_gmv
0 0 30-07-2022 mp000000006243574 7 1 0 0
1 1 30-07-2022 mp000000006292285 1 0 0 0
2 2 30-07-2022 mp000000006294016 18 1 0 0
3 3 31-07-2022 mp000000006243574 8 2 0 0
4 4 31-07-2022 mp000000006292285 5 0 0 0
5 5 31-07-2022 mp000000006294016 0 0 0 0
uj5u.com熱心網友回復:
首先將值轉換為日期時間以進行正確排序,通過組合 all和by創建MultiIndex和添加缺失的類別,最后轉換為原始格式:rpt_dateproduct_idDataFrame.reindexSeries.dt.strftime
df['rpt_date'] = pd.to_datetime(df['rpt_date'], dayfirst=True)
mux = pd.MultiIndex.from_product([df['rpt_date'].unique(), df['product_id'].unique()],
names=['rpt_date','product_id'])
df = df.set_index(['rpt_date','product_id']).reindex(mux, fill_value=0).reset_index()
df['rpt_date'] = df['rpt_date'].dt.strftime("%d-%m-%Y")
print (df)
rpt_date product_id total_views total_cart_adds \
0 30-07-2022 mp000000006243574 7 1
1 30-07-2022 mp000000006292285 1 0
2 30-07-2022 mp000000006294016 18 1
3 31-07-2022 mp000000006243574 8 2
4 31-07-2022 mp000000006292285 5 0
5 31-07-2022 mp000000006294016 0 0
total_order_unit total_gmv
0 0 0
1 0 0
2 0 0
3 0 0
4 0 0
5 0 0
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/507388.html
標籤:python-3.x 熊猫 麻木的
