pythonpandasapriori

How to presenting the results in the following manner:


This is my code:

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)

output:

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

but I want to present the result in the following manner: enter image description here

and actually I don't quite understand what's my code mean. Can anyone explain it to me? Thanks.


Solution

  • Pandas DataFrame is a two-dimensional tabular data representation, therefore I believe you can't achieve what you are trying to do with Pandas DataFrame. But If are thinking to represent it in a tabular way;

    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)
    

    If you're trying to get the desired output instead of using Pandas DataFrame simply try to solve with python loops. Replacing the last two lines from the first code snippet with the following line would give the desired output

    for data in results:
        for i in range(len(data)):
            print(Labels[i]+ ": "+ data[i])
        print("=========================================")