如何以下列方式呈现结果:
How to presenting the results in the following manner:
这是我的代码:
results=[]
for item in association_results:
pair = item[0]
items = [x for x in pair]
value0 = str(items[0])
value1 = str(items[1])
value2 = str(item[1])[:7]
value3 = str(item[2][0][2])[:7]
value4 = str(item[2][0][3])[:7]
rows = (value0,value1,value2,value3,value4)
results.append(rows)
Labels =['Title1','Title2','Support','Confidence','Lift']
store_suggestion = pd.DataFrame.from_records(results,columns=Labels)
print(store_suggestion)
输出:
Title1 Title2 Support Confidence Lift
0 chicken light cream 0.00453 0.29059 4.84395
Title1 Title2 Support Confidence Lift
0 chicken light cream 0.00453 0.29059 4.84395
1 escalope mushroom cream sauce 0.00573 0.30069 3.79083
Title1 Title2 Support Confidence Lift
0 chicken light cream 0.00453 0.29059 4.84395
1 escalope mushroom cream sauce 0.00573 0.30069 3.79083
2 escalope pasta 0.00586 0.37288 4.70081
Title1 Title2 Support Confidence Lift
0 chicken light cream 0.00453 0.29059 4.84395
1 escalope mushroom cream sauce 0.00573 0.30069 3.79083
2 escalope pasta 0.00586 0.37288 4.70081
3 herb & pepper ground beef 0.01599 0.32345 3.29199
Title1 Title2 Support Confidence Lift
0 chicken light cream 0.00453 0.29059 4.84395
1 escalope mushroom cream sauce 0.00573 0.30069 3.79083
2 escalope pasta 0.00586 0.37288 4.70081
3 herb & pepper ground beef 0.01599 0.32345 3.29199
4 tomato sauce ground beef 0.00533 0.37735 3.8406
但我想以下列方式呈现结果:
enter image description here
实际上我不太明白我的代码是什么意思。谁能给我解释一下?谢谢
Pandas DataFrame 是一种二维表格数据表示形式,因此我相信您无法使用 Pandas DataFrame 实现您想要做的事情。
但是如果想以表格的方式表示它;
for item in association_results:
pair = item[0]
items = [x for x in pair]
value0 = str(items[0])
value1 = str(items[1])
value2 = str(item[1])[:7]
value3 = str(item[2][0][2])[:7]
value4 = str(item[2][0][3])[:7]
value5 = value0 + " -> " + value1
rows = (value5,value2,value3,value4)
results.append(rows)
store_suggestion = pd.DataFrame.from_records(results,columns=Labels)
print(store_suggestion)
如果您试图获得所需的输出而不是使用 Pandas DataFrame,只需尝试使用 python 循环来解决。
将第一个代码片段的最后两行替换为以下行将提供所需的输出
for data in results:
for i in range(len(data)):
print(Labels[i]+ ": "+ data[i])
print("=========================================")
这是我的代码:
results=[]
for item in association_results:
pair = item[0]
items = [x for x in pair]
value0 = str(items[0])
value1 = str(items[1])
value2 = str(item[1])[:7]
value3 = str(item[2][0][2])[:7]
value4 = str(item[2][0][3])[:7]
rows = (value0,value1,value2,value3,value4)
results.append(rows)
Labels =['Title1','Title2','Support','Confidence','Lift']
store_suggestion = pd.DataFrame.from_records(results,columns=Labels)
print(store_suggestion)
输出:
Title1 Title2 Support Confidence Lift
0 chicken light cream 0.00453 0.29059 4.84395
Title1 Title2 Support Confidence Lift
0 chicken light cream 0.00453 0.29059 4.84395
1 escalope mushroom cream sauce 0.00573 0.30069 3.79083
Title1 Title2 Support Confidence Lift
0 chicken light cream 0.00453 0.29059 4.84395
1 escalope mushroom cream sauce 0.00573 0.30069 3.79083
2 escalope pasta 0.00586 0.37288 4.70081
Title1 Title2 Support Confidence Lift
0 chicken light cream 0.00453 0.29059 4.84395
1 escalope mushroom cream sauce 0.00573 0.30069 3.79083
2 escalope pasta 0.00586 0.37288 4.70081
3 herb & pepper ground beef 0.01599 0.32345 3.29199
Title1 Title2 Support Confidence Lift
0 chicken light cream 0.00453 0.29059 4.84395
1 escalope mushroom cream sauce 0.00573 0.30069 3.79083
2 escalope pasta 0.00586 0.37288 4.70081
3 herb & pepper ground beef 0.01599 0.32345 3.29199
4 tomato sauce ground beef 0.00533 0.37735 3.8406
但我想以下列方式呈现结果: enter image description here
实际上我不太明白我的代码是什么意思。谁能给我解释一下?谢谢
Pandas DataFrame 是一种二维表格数据表示形式,因此我相信您无法使用 Pandas DataFrame 实现您想要做的事情。 但是如果想以表格的方式表示它;
for item in association_results:
pair = item[0]
items = [x for x in pair]
value0 = str(items[0])
value1 = str(items[1])
value2 = str(item[1])[:7]
value3 = str(item[2][0][2])[:7]
value4 = str(item[2][0][3])[:7]
value5 = value0 + " -> " + value1
rows = (value5,value2,value3,value4)
results.append(rows)
store_suggestion = pd.DataFrame.from_records(results,columns=Labels)
print(store_suggestion)
如果您试图获得所需的输出而不是使用 Pandas DataFrame,只需尝试使用 python 循环来解决。 将第一个代码片段的最后两行替换为以下行将提供所需的输出
for data in results:
for i in range(len(data)):
print(Labels[i]+ ": "+ data[i])
print("=========================================")