1

enter image description here

In python dataframe while getting category codes after assigning a column to variable(y=df.column) is giving attribute error.

enter image description here.

While same is working fine if we directoly pass df.column to Categorical function.

enter image description here

3
  • 2
    Please don't add your code as pictures. See creating good pandas examples and create a minimal reproducible example Commented Jun 21, 2019 at 18:13
  • Rolling back your edit because it completely muddles up your post. Please copy paste the exact code in your images into your question, or not at all. Commented Jun 21, 2019 at 18:40
  • pd.Categorical(df.c1) gives arrays.categorical.Categorical object while series.Series object Commented Jan 9, 2023 at 23:10

1 Answer 1

4

The .cat attribute is a categorical accessor associated with categorical dtype Series:

s = pd.Series(['a', 'b', 'a']).astype('category') s 0 a 1 b 2 a dtype: category Categories (2, object): [a, b] s.cat.codes 0 0 1 1 2 0 dtype: int8 

OTOH, pd.Category returns a pandas.core.arrays.categorical.Categorical object, which has these attributes defined on the object directly:

pd.Categorical(['a', 'b', 'c']) # [a, b, c] pd.Categorical(['a', 'b', 'c']) .codes # array([0, 1, 2], dtype=int8) 
Sign up to request clarification or add additional context in comments.

2 Comments

while printing 'y' and 'df.c1' both seems to be as categorical type
@VikashYadav Please carefully look at your code and images, they do not corroborate. Anyway, the whole point of my answer, is that df['c1'] is a Series, while pd.Categorical(...) (and anything you assign it to) is a Categorical object.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.