将类别列表打印为一列
printing list of categories as a column
我正在使用 pandas 文档中的示例。假设我在阅读 excel 文件后有一个系列
import pandas as pd
s = pd.Series(["a","b","c","a"], dtype="category")
我知道我可以通过
获得不同的类别
scat=s.cat.categories
print scat
为此我得到了
Index([u'a', u'b', u'c'], dtype='object')
我想知道使此列表显示为一列的好方法是什么。像
a
b
c
我可以通过 np.asarray
摆脱 u'
但仍然没有得到我需要的格式。
我觉得没问题-'u'
表示unicode
字符串:
s = pd.Series(["a","b","c","a"], dtype="category")
print s
0 a
1 b
2 c
3 a
dtype: category
Categories (3, object): [a, b, c]
scat=s.cat.categories
print scat
Index([u'a', u'b', u'c'], dtype='object')
print scat[0]
a
print type(scat[0])
<type 'str'>
如果你想不循环打印列使用numpy reshape
:
print len(scat)
3
print scat.values.reshape(len(scat),1)
[['a']
['b']
['c']]
当你说 'appear' 作为列时,我不确定你的意思。
您可以通过以下方式创建列表而不是索引:
>>> s.cat.categories.tolist()
['a', 'b', 'c']
或者您可以简单地使用 for 循环将它们打印在列结构中:
for c in s.cat.categories:
print c
a
b
c
或者您可以创建一个系列(或数据框):
>>> 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
我正在使用 pandas 文档中的示例。假设我在阅读 excel 文件后有一个系列
import pandas as pd
s = pd.Series(["a","b","c","a"], dtype="category")
我知道我可以通过
获得不同的类别scat=s.cat.categories
print scat
为此我得到了
Index([u'a', u'b', u'c'], dtype='object')
我想知道使此列表显示为一列的好方法是什么。像
a
b
c
我可以通过 np.asarray
摆脱 u'
但仍然没有得到我需要的格式。
我觉得没问题-'u'
表示unicode
字符串:
s = pd.Series(["a","b","c","a"], dtype="category")
print s
0 a
1 b
2 c
3 a
dtype: category
Categories (3, object): [a, b, c]
scat=s.cat.categories
print scat
Index([u'a', u'b', u'c'], dtype='object')
print scat[0]
a
print type(scat[0])
<type 'str'>
如果你想不循环打印列使用numpy reshape
:
print len(scat)
3
print scat.values.reshape(len(scat),1)
[['a']
['b']
['c']]
当你说 'appear' 作为列时,我不确定你的意思。
您可以通过以下方式创建列表而不是索引:
>>> s.cat.categories.tolist()
['a', 'b', 'c']
或者您可以简单地使用 for 循环将它们打印在列结构中:
for c in s.cat.categories:
print c
a
b
c
或者您可以创建一个系列(或数据框):
>>> 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