如何使用python-panda为下面的代码片段创建矩阵形式的输出


Channel_list=['Retail' 'Hotel']
Region=['Other' 'Lisbon' 'Oporto']
temp=0
rows=[]
column=[]
for ele1 in Channel_list:
for ele2 in Region:
for i in WS_Customer.columns[3:]:
temp=temp+WS_Customer[i].where(WS_Customer['Region']==ele2 ).where(WS_Customer['Channel']==ele1).mean()
column.append(temp)
column

上述输出将类似

[47004.971428571414,
94142.24920634918,
138138.98604845445,
165352.62111954446,
191426.21433988342,
217110.142911312]           

但我需要创建一个2x3矩阵(表(,其中列为Channel_list,行为Region。有人能帮我创建这个吗。

table = np.array(column).reshape((len(Region), -1))
pd.DataFrame(table, index=Region, columns=Channel_list)
#                Other         Lisbon         Oporto
#Retail   47004.971429   94142.249206  138138.986048
#Hotel   165352.621120  191426.214340  217110.142911

我几乎可以肯定,您混淆了Region和Channel_list的名称。

最新更新