我希望在对两列进行分组时填充空存储桶。这是设置和结果数据框:
bins = [-np.inf,0,10000,20000,40000,60000,80000,100000,np.inf] # include infinity value as the endpoint to the bins
labels = ['0K','10K', '20K','40K','60K', '80K','100K','> 100K']
df_Done_Major['Bucket'] = pd.cut(df_Done['rfq_qty'], bins=bins, labels=labels)
# Polpulate empty buckets with 0 adjacently
df_Done_Major_Fill_Empty_Bucket = df_Done_Major.groupby(['Bucket', 'currency_str'], as_index=False)['Bucket'].size()
预期的结果df:
display(df_Done_Major_Fill_Empty_Bucket)
Bucket currency_str count
10K AUD 9
60K AUD 1
60K USD 1
100K AUD 1
100K USD 1
> 100K AUD 57
> 100K CAD 4
> 100K USD 5
所需结果:
currency_str Bucket Count
AUD 0K 9
10K 0
20K 0
40K 0
60K 1
80K 0
100K 1
> 100K 57
USD 0K 0
10K 0
20K 0
40K 0
60K 1
80K 0
100K 1
> 100K 5
CAD 0K 0
10K 0
20K 0
40K 0
60K 0
80K 0
100K 0
> 100K 4
我的尝试
mux = pd.MultiIndex.from_product([df_Done_Major_Fill_Empty_Bucket.index.levels[1], df_Done_Major['Bucket'].cat.categories])
df_Done_Major_Fill_Empty_Bucket = df_Done_Major_Fill_Empty_Bucket.reindex(mux, fill_value=0)
标签上没有任何计数。关于如何返回每笔交易的计数有什么建议吗?
display(df_Done_Major_Fill_Empty_Bucket)
AUD 0K 0
10K 0
20K 0
40K 0
60K 0
80K 0
100K 0
> 100K 0
CAD 0K 0
10K 0
20K 0
40K 0
60K 0
80K 0
100K 0
> 100K 0
EUR 0K 0
10K 0
.
.
.
答案 0 :(得分:0)
这是与所引用的分组和索引有关的问题
df_Done_Major_Fill_Empty_Bucket = df_Done_Major.groupby(['currency_str','Bucket'], as_index=False)['Bucket'].size()
mux = pd.MultiIndex.from_product([df_Done_Major_Fill_Empty_Bucket.index.levels[0], df_Done_Major['Bucket'].cat.categories])