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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With