Return category labels for a categorical series
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
add a comment |
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
it is because the values are on the fourth column, how can I extract the non-repeated values in an array?
– Little
Nov 11 '18 at 23:11
your data are already category? There is no problem with what you are doing
– Khalil Al Hooti
Nov 11 '18 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 '18 at 23:13
1
dtype category helps decrease memory use typedata.info(verbose=True)
before and after changing dtype to category and check How much computer memory is saved!
– Khalil Al Hooti
Nov 11 '18 at 23:17
add a comment |
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
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
python pandas series
edited Nov 12 '18 at 4:59
smj
1,111613
1,111613
asked Nov 11 '18 at 23:01
LittleLittle
95852245
95852245
it is because the values are on the fourth column, how can I extract the non-repeated values in an array?
– Little
Nov 11 '18 at 23:11
your data are already category? There is no problem with what you are doing
– Khalil Al Hooti
Nov 11 '18 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 '18 at 23:13
1
dtype category helps decrease memory use typedata.info(verbose=True)
before and after changing dtype to category and check How much computer memory is saved!
– Khalil Al Hooti
Nov 11 '18 at 23:17
add a comment |
it is because the values are on the fourth column, how can I extract the non-repeated values in an array?
– Little
Nov 11 '18 at 23:11
your data are already category? There is no problem with what you are doing
– Khalil Al Hooti
Nov 11 '18 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 '18 at 23:13
1
dtype category helps decrease memory use typedata.info(verbose=True)
before and after changing dtype to category and check How much computer memory is saved!
– Khalil Al Hooti
Nov 11 '18 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 '18 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 '18 at 23:11
your data are already category? There is no problem with what you are doing
– Khalil Al Hooti
Nov 11 '18 at 23:11
your data are already category? There is no problem with what you are doing
– Khalil Al Hooti
Nov 11 '18 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 '18 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 '18 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 '18 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 '18 at 23:17
add a comment |
1 Answer
1
active
oldest
votes
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.
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 11 '18 at 23:33
smjsmj
1,111613
1,111613
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
it is because the values are on the fourth column, how can I extract the non-repeated values in an array?
– Little
Nov 11 '18 at 23:11
your data are already category? There is no problem with what you are doing
– Khalil Al Hooti
Nov 11 '18 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 '18 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 '18 at 23:17