create a heatmap of two categorical variablesMaking heatmap from pandas DataFrameHow to merge two dictionaries in a single expression?Are static class variables possible?How can I safely create a nested directory in Python?How to return multiple values from a function?Using global variables in a functionLimiting floats to two decimal pointsHow do I pass a variable by reference?How do I concatenate two lists in Python?Create a dictionary with list comprehension in PythonHow to access environment variable values?

What food production methods would allow a metropolis like New York to become self sufficient

Company stopped paying my salary. What are my options?

What does this quote in Small Gods refer to?

When do you stop "pushing" a book?

Are there variations of the regular runtimes of the Big-O-Notation?

Is a vertical stabiliser needed for straight line flight in a glider?

Why are parallelograms defined as quadrilaterals? What term would encompass polygons with greater than two parallel pairs?

Bigger equation in text-mode math

Two researchers want to work on the same extension to my paper. Who to help?

Pre-1993 comic in which Wolverine's claws were turned to rubber?

Why is the Sun made of light elements only?

How is CoreiX like Corei5, i7 is related to Haswell, Ivy Bridge?

Is every story set in the future "science fiction"?

Detect the first rising edge of 3 input signals

Noob at soldering, can anyone explain why my circuit wont work?

Company threw a surprise party for the CEO, 3 weeks later management says we have to pay for it, do I have to?

How to evaluate sum with one million summands?

Can 'sudo apt-get remove [write]' destroy my Ubuntu?

Was Mohammed the most popular first name for boys born in Berlin in 2018?

date to display the EDT time

Why do unstable nuclei form?

What do "KAL." and "A.S." stand for in this inscription?

Passport stamps art, can it be done?

Why was wildfire not used during the Battle of Winterfell?



create a heatmap of two categorical variables


Making heatmap from pandas DataFrameHow to merge two dictionaries in a single expression?Are static class variables possible?How can I safely create a nested directory in Python?How to return multiple values from a function?Using global variables in a functionLimiting floats to two decimal pointsHow do I pass a variable by reference?How do I concatenate two lists in Python?Create a dictionary with list comprehension in PythonHow to access environment variable values?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















I have the following datasets of three variables:




  1. df['Score'] Float dummy (1 or 0)

  2. df['Province'] an object column where each row is a region

  3. df['Product type'] an object indicating the industry.



I would like to create a jointplot where on the x axis I have the different industries, on the y axis the different provinces and as colours of my jointplot I have the relative frequency of the score.
Something like this.
https://seaborn.pydata.org/examples/hexbin_marginals.html



For the time being, I could only do the following



mean = df.groupby(['Province', 'Product type'])['score'].mean()


But i am not sure how to plot it.



Thanks!










share|improve this question



















  • 1





    Possible duplicate of Making heatmap from pandas DataFrame

    – perl
    Mar 23 at 10:17

















0















I have the following datasets of three variables:




  1. df['Score'] Float dummy (1 or 0)

  2. df['Province'] an object column where each row is a region

  3. df['Product type'] an object indicating the industry.



I would like to create a jointplot where on the x axis I have the different industries, on the y axis the different provinces and as colours of my jointplot I have the relative frequency of the score.
Something like this.
https://seaborn.pydata.org/examples/hexbin_marginals.html



For the time being, I could only do the following



mean = df.groupby(['Province', 'Product type'])['score'].mean()


But i am not sure how to plot it.



Thanks!










share|improve this question



















  • 1





    Possible duplicate of Making heatmap from pandas DataFrame

    – perl
    Mar 23 at 10:17













0












0








0








I have the following datasets of three variables:




  1. df['Score'] Float dummy (1 or 0)

  2. df['Province'] an object column where each row is a region

  3. df['Product type'] an object indicating the industry.



I would like to create a jointplot where on the x axis I have the different industries, on the y axis the different provinces and as colours of my jointplot I have the relative frequency of the score.
Something like this.
https://seaborn.pydata.org/examples/hexbin_marginals.html



For the time being, I could only do the following



mean = df.groupby(['Province', 'Product type'])['score'].mean()


But i am not sure how to plot it.



Thanks!










share|improve this question
















I have the following datasets of three variables:




  1. df['Score'] Float dummy (1 or 0)

  2. df['Province'] an object column where each row is a region

  3. df['Product type'] an object indicating the industry.



I would like to create a jointplot where on the x axis I have the different industries, on the y axis the different provinces and as colours of my jointplot I have the relative frequency of the score.
Something like this.
https://seaborn.pydata.org/examples/hexbin_marginals.html



For the time being, I could only do the following



mean = df.groupby(['Province', 'Product type'])['score'].mean()


But i am not sure how to plot it.



Thanks!







python pandas plot






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 23 at 10:00









N8888

4561513




4561513










asked Mar 23 at 9:57









Filippo SebastioFilippo Sebastio

19819




19819







  • 1





    Possible duplicate of Making heatmap from pandas DataFrame

    – perl
    Mar 23 at 10:17












  • 1





    Possible duplicate of Making heatmap from pandas DataFrame

    – perl
    Mar 23 at 10:17







1




1





Possible duplicate of Making heatmap from pandas DataFrame

– perl
Mar 23 at 10:17





Possible duplicate of Making heatmap from pandas DataFrame

– perl
Mar 23 at 10:17












1 Answer
1






active

oldest

votes


















0














If you are looking for a heatmap, you could use seaborn heatmap function. However you need to pivot your table first.



Just creating a small example:



import numpy as np 
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

score = [1, 1, 1, 0, 1, 0, 0, 0]
provinces = ['Place1' ,'Place2' ,'Place2', 'Place3','Place1', 'Place2','Place3','Place1']
products = ['Product1' ,'Product3' ,'Product2', 'Product2','Product1', 'Product2','Product1','Product1']
df = pd.DataFrame('Province': provinces,
'Product type': products,
'score': score
)


My df looks like:



 'Province''Product type''score'
0 Place1 Product1 1
1 Place2 Product3 1
2 Place2 Product2 1
3 Place3 Product2 0
4 Place1 Product1 1
5 Place2 Product2 0
6 Place3 Product1 0
7 Place1 Product1 0


Then:



df_heatmap = df.pivot_table(values='score',index='Province',columns='Product type',aggfunc=np.mean)
sns.heatmap(df_heatmap,annot=True)
plt.show()


The result is:








share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55312548%2fcreate-a-heatmap-of-two-categorical-variables%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    If you are looking for a heatmap, you could use seaborn heatmap function. However you need to pivot your table first.



    Just creating a small example:



    import numpy as np 
    import pandas as pd
    import seaborn as sns
    import matplotlib.pyplot as plt

    score = [1, 1, 1, 0, 1, 0, 0, 0]
    provinces = ['Place1' ,'Place2' ,'Place2', 'Place3','Place1', 'Place2','Place3','Place1']
    products = ['Product1' ,'Product3' ,'Product2', 'Product2','Product1', 'Product2','Product1','Product1']
    df = pd.DataFrame('Province': provinces,
    'Product type': products,
    'score': score
    )


    My df looks like:



     'Province''Product type''score'
    0 Place1 Product1 1
    1 Place2 Product3 1
    2 Place2 Product2 1
    3 Place3 Product2 0
    4 Place1 Product1 1
    5 Place2 Product2 0
    6 Place3 Product1 0
    7 Place1 Product1 0


    Then:



    df_heatmap = df.pivot_table(values='score',index='Province',columns='Product type',aggfunc=np.mean)
    sns.heatmap(df_heatmap,annot=True)
    plt.show()


    The result is:








    share|improve this answer



























      0














      If you are looking for a heatmap, you could use seaborn heatmap function. However you need to pivot your table first.



      Just creating a small example:



      import numpy as np 
      import pandas as pd
      import seaborn as sns
      import matplotlib.pyplot as plt

      score = [1, 1, 1, 0, 1, 0, 0, 0]
      provinces = ['Place1' ,'Place2' ,'Place2', 'Place3','Place1', 'Place2','Place3','Place1']
      products = ['Product1' ,'Product3' ,'Product2', 'Product2','Product1', 'Product2','Product1','Product1']
      df = pd.DataFrame('Province': provinces,
      'Product type': products,
      'score': score
      )


      My df looks like:



       'Province''Product type''score'
      0 Place1 Product1 1
      1 Place2 Product3 1
      2 Place2 Product2 1
      3 Place3 Product2 0
      4 Place1 Product1 1
      5 Place2 Product2 0
      6 Place3 Product1 0
      7 Place1 Product1 0


      Then:



      df_heatmap = df.pivot_table(values='score',index='Province',columns='Product type',aggfunc=np.mean)
      sns.heatmap(df_heatmap,annot=True)
      plt.show()


      The result is:








      share|improve this answer

























        0












        0








        0







        If you are looking for a heatmap, you could use seaborn heatmap function. However you need to pivot your table first.



        Just creating a small example:



        import numpy as np 
        import pandas as pd
        import seaborn as sns
        import matplotlib.pyplot as plt

        score = [1, 1, 1, 0, 1, 0, 0, 0]
        provinces = ['Place1' ,'Place2' ,'Place2', 'Place3','Place1', 'Place2','Place3','Place1']
        products = ['Product1' ,'Product3' ,'Product2', 'Product2','Product1', 'Product2','Product1','Product1']
        df = pd.DataFrame('Province': provinces,
        'Product type': products,
        'score': score
        )


        My df looks like:



         'Province''Product type''score'
        0 Place1 Product1 1
        1 Place2 Product3 1
        2 Place2 Product2 1
        3 Place3 Product2 0
        4 Place1 Product1 1
        5 Place2 Product2 0
        6 Place3 Product1 0
        7 Place1 Product1 0


        Then:



        df_heatmap = df.pivot_table(values='score',index='Province',columns='Product type',aggfunc=np.mean)
        sns.heatmap(df_heatmap,annot=True)
        plt.show()


        The result is:








        share|improve this answer













        If you are looking for a heatmap, you could use seaborn heatmap function. However you need to pivot your table first.



        Just creating a small example:



        import numpy as np 
        import pandas as pd
        import seaborn as sns
        import matplotlib.pyplot as plt

        score = [1, 1, 1, 0, 1, 0, 0, 0]
        provinces = ['Place1' ,'Place2' ,'Place2', 'Place3','Place1', 'Place2','Place3','Place1']
        products = ['Product1' ,'Product3' ,'Product2', 'Product2','Product1', 'Product2','Product1','Product1']
        df = pd.DataFrame('Province': provinces,
        'Product type': products,
        'score': score
        )


        My df looks like:



         'Province''Product type''score'
        0 Place1 Product1 1
        1 Place2 Product3 1
        2 Place2 Product2 1
        3 Place3 Product2 0
        4 Place1 Product1 1
        5 Place2 Product2 0
        6 Place3 Product1 0
        7 Place1 Product1 0


        Then:



        df_heatmap = df.pivot_table(values='score',index='Province',columns='Product type',aggfunc=np.mean)
        sns.heatmap(df_heatmap,annot=True)
        plt.show()


        The result is:









        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 23 at 15:13









        vmouffronvmouffron

        9016




        9016





























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55312548%2fcreate-a-heatmap-of-two-categorical-variables%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

            Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

            Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript