如何使用python为下面的代码段创建矩阵形式的输出
上面的输出将如下所示如何使用python为下面的代码段创建矩阵形式的输出,python,python-3.x,pandas,numpy,dataframe,Python,Python 3.x,Pandas,Numpy,Dataframe,上面的输出将如下所示 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[
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
但我需要创建一个2x3矩阵(表),列作为通道列表,行作为区域。
有人能帮我创造这个吗
[47004.971428571414,
94142.24920634918,
138138.98604845445,
165352.62111954446,
191426.21433988342,
217110.142911312]
我几乎可以肯定,您混淆了地区和频道列表名称。如果您将
WS\u Customer
直接转换到所需的表中,您可能会得到更好的结果。@DYZ您的答案有助于创建数据框,谢谢!!但输出不符合预期,平均值未放在正确位置。所有的值都混淆了。请详细说明:显示预期的输出。@DYZ,谢谢!!。我对数据帧的创建做了一些小的修改,效果非常好。
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