.str
numpy支持多种数据类型,但字符串都是存储为object_类型的。str则是一个有用的中间类型。
>>> s = pd.Series(['a_b','b_c','c_d'],dtype = 'object')
>>> s
0 a_b
1 b_c
2 c_d
dtype: object
>>> s.str.split("_",expand=True) # 相当于分列了
0 1
0 a b
1 b c
2 c d
>>> s.str.split("_",expand=False)
0 [a, b]
1 [b, c]
2 [c, d]
dtype: object
.cat
处理类别。
>>> s = pd.Series( [1,2,3], dtype = 'category')
>>> s
0 1
1 2
2 3
dtype: category
Categories (3, int64): [1, 2, 3]
>>> s.cat.categories
Int64Index([1, 2, 3], dtype='int64')
.dt
处理时间格式。
>>> s = pd.Series(['2017-08-01','2017-08-03','2017-08-03'], dtype = 'datetime64[ns]')
>>> s
0 2017-08-01
1 2017-08-03
2 2017-08-03
dtype: datetime64[ns]
>>> s.dt.month
0 8
1 8
2 8
dtype: int64