Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

printing list of categories as a column

I am taking example from pandas documentation. Let us say I have a series after reading an excel file

import pandas as pd
s = pd.Series(["a","b","c","a"], dtype="category")

I know I can get different categories by

scat=s.cat.categories
print scat

For which I get

Index([u'a', u'b', u'c'], dtype='object')

I was wondering what is a good way to make this list appear as a column. Something like

a
b
c

I could get rid of u' by doing np.asarray but still do not get the format I need.

like image 272
PagMax Avatar asked Feb 11 '16 06:02

PagMax


People also ask

How do I print a list of columns in a data frame?

To access the names of a Pandas dataframe, we can the method columns(). For example, if our dataframe is called df we just type print(df. columns) to get all the columns of the Pandas dataframe.

How do I print a list of column names?

To get all column name you can iterate over the data_all2. columns . You will get all column names. Or you can store all column names to another list variable and then print list.

How do I print the data type of each column in pandas?

Use Dataframe. dtypes to get Data types of columns in Dataframe. In Python's pandas module Dataframe class provides an attribute to get the data type information of each columns i.e. It returns a series object containing data type information of each column.


1 Answers

I'm not sure by what you mean when you say 'appear' as a column.

You can create a list instead of an index via:

>>> s.cat.categories.tolist()
['a', 'b', 'c']

Or you can simply print them out in a column structure using a for loop:

for c in s.cat.categories:
    print c

a
b
c

Or you could create a series (or dataframe):

>>> pd.Series(s.cat.categories)
0    a
1    b
2    c
dtype: object

>>> pd.DataFrame(s.cat.categories)
   0
0  a
1  b
2  c
like image 200
Alexander Avatar answered Nov 02 '22 23:11

Alexander