Search code examples
python-3.xpandasdataframecolor-schemepoker

How to color code pandas dataframe according to frequency of elements?


I like to code and I like to play poker! So I am trying to make a program to keep track of my poker statistics.

I made a poker hand range table using pandas, however I am unable to color-code it according to the hands I tend to 3-bet more as compared to others.

Currently my dataframe looks like this:

enter image description here

I have list of hands that I want to plot in this chart(dataframe) according to their frequency. Let's say:

Hands = ["A A","K K","A Ks"....]

To give an idea I want my chart to look somewhat like this:

enter image description here

Here Yellow show hands that I 3-bet often, red shows the hands I 3-bet less often than Yellow and grey are hands I 3-bet rarely.

EDIT

I was able to get the result I wanted. Check out the answer section.


Solution

  • So I simply calculated the percentage of hands that I 3-bet and stored it in a dictionary

    freq_percentage = dict()
    for i in freq_each:
        if(i in freq_each_raised):
            freq_percentage[i] = freq_each_raised[i]/freq_each[i]
    

    Here freq_each stores frequency of each hand and freq_each_raised stores frequency of each hand that was raised and freq_percentage stores percentage of time I raised a particular hand.

    matrix = [[0 for i in range(13)] for j in range(13)]
    
    for i in freq_percentage:
        for m in range(13):
            b = 0
            for n in range(13):
                if(chart[m][n]==i):
                    matrix[m][n]=round(freq_percentage[i],2)
                    b = 1
                    break
            if(b):
                break
    
    hands_percentage = pd.DataFrame(matrix)
    plt.figure(figsize=(14,7))
    axs = sns.heatmap(hands_percentage, annot = chart, fmt = '',xticklabels=False, yticklabels=False)
    

    enter image description here