Return category labels for a categorical series











up vote
0
down vote

favorite
1












I am using pandas for extracting the categories of a dataset like in the following code:



data=pd.read_csv("iris.csv",header=None)
data[4]=data[4].astype("category")


When I print the values of data[4] I got this list:



1         Setosa
2 Setosa
3 Setosa
4 Setosa
5 Setosa
6 Setosa
7 Setosa
...
149 Virginica
150 Virginica
Name: 4, Length: 150, dtype: category
Categories (3, object): [Setosa, Versicolor, Virginica]


but I want to have the three categories in an array so to have something like:



[Setosa, Versicolor, Virginica]


I was looking around, but I could not find anything that could serve.



Any help?










share|improve this question
























  • it is because the values are on the fourth column, how can I extract the non-repeated values in an array?
    – Little
    Nov 11 at 23:11










  • your data are already category? There is no problem with what you are doing
    – Khalil Al Hooti
    Nov 11 at 23:11










  • the problem is that I do not know how to call the categories values, because it prints all. I would like to do something like data[4].getcategories() and return an array of [s,v,vi]
    – Little
    Nov 11 at 23:13






  • 1




    dtype category helps decrease memory use type data.info(verbose=True) before and after changing dtype to category and check How much computer memory is saved!
    – Khalil Al Hooti
    Nov 11 at 23:17

















up vote
0
down vote

favorite
1












I am using pandas for extracting the categories of a dataset like in the following code:



data=pd.read_csv("iris.csv",header=None)
data[4]=data[4].astype("category")


When I print the values of data[4] I got this list:



1         Setosa
2 Setosa
3 Setosa
4 Setosa
5 Setosa
6 Setosa
7 Setosa
...
149 Virginica
150 Virginica
Name: 4, Length: 150, dtype: category
Categories (3, object): [Setosa, Versicolor, Virginica]


but I want to have the three categories in an array so to have something like:



[Setosa, Versicolor, Virginica]


I was looking around, but I could not find anything that could serve.



Any help?










share|improve this question
























  • it is because the values are on the fourth column, how can I extract the non-repeated values in an array?
    – Little
    Nov 11 at 23:11










  • your data are already category? There is no problem with what you are doing
    – Khalil Al Hooti
    Nov 11 at 23:11










  • the problem is that I do not know how to call the categories values, because it prints all. I would like to do something like data[4].getcategories() and return an array of [s,v,vi]
    – Little
    Nov 11 at 23:13






  • 1




    dtype category helps decrease memory use type data.info(verbose=True) before and after changing dtype to category and check How much computer memory is saved!
    – Khalil Al Hooti
    Nov 11 at 23:17















up vote
0
down vote

favorite
1









up vote
0
down vote

favorite
1






1





I am using pandas for extracting the categories of a dataset like in the following code:



data=pd.read_csv("iris.csv",header=None)
data[4]=data[4].astype("category")


When I print the values of data[4] I got this list:



1         Setosa
2 Setosa
3 Setosa
4 Setosa
5 Setosa
6 Setosa
7 Setosa
...
149 Virginica
150 Virginica
Name: 4, Length: 150, dtype: category
Categories (3, object): [Setosa, Versicolor, Virginica]


but I want to have the three categories in an array so to have something like:



[Setosa, Versicolor, Virginica]


I was looking around, but I could not find anything that could serve.



Any help?










share|improve this question















I am using pandas for extracting the categories of a dataset like in the following code:



data=pd.read_csv("iris.csv",header=None)
data[4]=data[4].astype("category")


When I print the values of data[4] I got this list:



1         Setosa
2 Setosa
3 Setosa
4 Setosa
5 Setosa
6 Setosa
7 Setosa
...
149 Virginica
150 Virginica
Name: 4, Length: 150, dtype: category
Categories (3, object): [Setosa, Versicolor, Virginica]


but I want to have the three categories in an array so to have something like:



[Setosa, Versicolor, Virginica]


I was looking around, but I could not find anything that could serve.



Any help?







python pandas series






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 at 4:59









smj

1,076613




1,076613










asked Nov 11 at 23:01









Little

94052145




94052145












  • it is because the values are on the fourth column, how can I extract the non-repeated values in an array?
    – Little
    Nov 11 at 23:11










  • your data are already category? There is no problem with what you are doing
    – Khalil Al Hooti
    Nov 11 at 23:11










  • the problem is that I do not know how to call the categories values, because it prints all. I would like to do something like data[4].getcategories() and return an array of [s,v,vi]
    – Little
    Nov 11 at 23:13






  • 1




    dtype category helps decrease memory use type data.info(verbose=True) before and after changing dtype to category and check How much computer memory is saved!
    – Khalil Al Hooti
    Nov 11 at 23:17




















  • it is because the values are on the fourth column, how can I extract the non-repeated values in an array?
    – Little
    Nov 11 at 23:11










  • your data are already category? There is no problem with what you are doing
    – Khalil Al Hooti
    Nov 11 at 23:11










  • the problem is that I do not know how to call the categories values, because it prints all. I would like to do something like data[4].getcategories() and return an array of [s,v,vi]
    – Little
    Nov 11 at 23:13






  • 1




    dtype category helps decrease memory use type data.info(verbose=True) before and after changing dtype to category and check How much computer memory is saved!
    – Khalil Al Hooti
    Nov 11 at 23:17


















it is because the values are on the fourth column, how can I extract the non-repeated values in an array?
– Little
Nov 11 at 23:11




it is because the values are on the fourth column, how can I extract the non-repeated values in an array?
– Little
Nov 11 at 23:11












your data are already category? There is no problem with what you are doing
– Khalil Al Hooti
Nov 11 at 23:11




your data are already category? There is no problem with what you are doing
– Khalil Al Hooti
Nov 11 at 23:11












the problem is that I do not know how to call the categories values, because it prints all. I would like to do something like data[4].getcategories() and return an array of [s,v,vi]
– Little
Nov 11 at 23:13




the problem is that I do not know how to call the categories values, because it prints all. I would like to do something like data[4].getcategories() and return an array of [s,v,vi]
– Little
Nov 11 at 23:13




1




1




dtype category helps decrease memory use type data.info(verbose=True) before and after changing dtype to category and check How much computer memory is saved!
– Khalil Al Hooti
Nov 11 at 23:17






dtype category helps decrease memory use type data.info(verbose=True) before and after changing dtype to category and check How much computer memory is saved!
– Khalil Al Hooti
Nov 11 at 23:17














1 Answer
1






active

oldest

votes

















up vote
0
down vote













data[4].cat.categories.values might be superior here, versus .unique().



Take a look at https://pandas.pydata.org/pandas-docs/stable/categorical.html, "Working with categories".




Note: The result of unique() is not always the same as
Series.cat.categories, because Series.unique() has a couple of
guarantees, namely that it returns categories in the order of
appearance, and it only includes values that are actually present.




.cat.categories.values seems much faster, because of the reasons listed above I imagine.



Example:



import pandas as pd
import numpy as np

s = pd.Series(np.random.choice(['a', 'b', 'c'], 1000), dtype = "category")

% timeit a = s.unique()

# 303 µs ± 23.2 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

% timeit b = s.cat.categories.values

# 1.26 µs ± 27.9 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

print(s.cat.categories.values)

# ['a' 'b' 'c']


The size of your dataset and other requirements will likely determine which is better.






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%2f53254112%2freturn-category-labels-for-a-categorical-series%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








    up vote
    0
    down vote













    data[4].cat.categories.values might be superior here, versus .unique().



    Take a look at https://pandas.pydata.org/pandas-docs/stable/categorical.html, "Working with categories".




    Note: The result of unique() is not always the same as
    Series.cat.categories, because Series.unique() has a couple of
    guarantees, namely that it returns categories in the order of
    appearance, and it only includes values that are actually present.




    .cat.categories.values seems much faster, because of the reasons listed above I imagine.



    Example:



    import pandas as pd
    import numpy as np

    s = pd.Series(np.random.choice(['a', 'b', 'c'], 1000), dtype = "category")

    % timeit a = s.unique()

    # 303 µs ± 23.2 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

    % timeit b = s.cat.categories.values

    # 1.26 µs ± 27.9 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

    print(s.cat.categories.values)

    # ['a' 'b' 'c']


    The size of your dataset and other requirements will likely determine which is better.






    share|improve this answer

























      up vote
      0
      down vote













      data[4].cat.categories.values might be superior here, versus .unique().



      Take a look at https://pandas.pydata.org/pandas-docs/stable/categorical.html, "Working with categories".




      Note: The result of unique() is not always the same as
      Series.cat.categories, because Series.unique() has a couple of
      guarantees, namely that it returns categories in the order of
      appearance, and it only includes values that are actually present.




      .cat.categories.values seems much faster, because of the reasons listed above I imagine.



      Example:



      import pandas as pd
      import numpy as np

      s = pd.Series(np.random.choice(['a', 'b', 'c'], 1000), dtype = "category")

      % timeit a = s.unique()

      # 303 µs ± 23.2 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

      % timeit b = s.cat.categories.values

      # 1.26 µs ± 27.9 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

      print(s.cat.categories.values)

      # ['a' 'b' 'c']


      The size of your dataset and other requirements will likely determine which is better.






      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        data[4].cat.categories.values might be superior here, versus .unique().



        Take a look at https://pandas.pydata.org/pandas-docs/stable/categorical.html, "Working with categories".




        Note: The result of unique() is not always the same as
        Series.cat.categories, because Series.unique() has a couple of
        guarantees, namely that it returns categories in the order of
        appearance, and it only includes values that are actually present.




        .cat.categories.values seems much faster, because of the reasons listed above I imagine.



        Example:



        import pandas as pd
        import numpy as np

        s = pd.Series(np.random.choice(['a', 'b', 'c'], 1000), dtype = "category")

        % timeit a = s.unique()

        # 303 µs ± 23.2 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

        % timeit b = s.cat.categories.values

        # 1.26 µs ± 27.9 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

        print(s.cat.categories.values)

        # ['a' 'b' 'c']


        The size of your dataset and other requirements will likely determine which is better.






        share|improve this answer












        data[4].cat.categories.values might be superior here, versus .unique().



        Take a look at https://pandas.pydata.org/pandas-docs/stable/categorical.html, "Working with categories".




        Note: The result of unique() is not always the same as
        Series.cat.categories, because Series.unique() has a couple of
        guarantees, namely that it returns categories in the order of
        appearance, and it only includes values that are actually present.




        .cat.categories.values seems much faster, because of the reasons listed above I imagine.



        Example:



        import pandas as pd
        import numpy as np

        s = pd.Series(np.random.choice(['a', 'b', 'c'], 1000), dtype = "category")

        % timeit a = s.unique()

        # 303 µs ± 23.2 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

        % timeit b = s.cat.categories.values

        # 1.26 µs ± 27.9 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

        print(s.cat.categories.values)

        # ['a' 'b' 'c']


        The size of your dataset and other requirements will likely determine which is better.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 11 at 23:33









        smj

        1,076613




        1,076613






























            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.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • 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%2f53254112%2freturn-category-labels-for-a-categorical-series%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

            Florida Star v. B. J. F.

            Danny Elfman

            Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues