ndarray类型(来自numpy)
n维数据类型
关注数据的结构表达(数据的维度)
ndarray的切片和索引
ndarray的计算
Series类型(来自pandas)
一维
关注数据与索引的关系
自动索引
自定义索引(index)
两套索引并存,但不能混用
Series类型通过自定义索引进行切片和索引,与ndarray类型用法相同
*可以由字典创建,字典的键自动转换位Series的索引
Series类型的操作
.index
.value
.name
Series类型的操作
DateFrame(来自pandas)
二维(也可以表达多维,但不常用)
关注数据与索引的关系
定义:共用相同索引的数据
行索引:index
列索引:columns
DataFrame类型的索引和切片
有4种方式
x = np.arange(24).reshape(4,6)
y = pd.DataFrame(x,index = ['A','B','C','D'],columns = ['a','b','c','d','e','f'])
print(y,'\n')
>>>
a b c d e f
A 0 1 2 3 4 5
B 6 7 8 9 10 11
C 12 13 14 15 16 17
D 18 19 20 21 22 23
y[]
y.loc[]
y.iloc[]
y.ix[]
其中,
y[]只能用来按行切片,或者获取某列数据;
y.loc[]利用自定义行索引和列索引进行切片或数据选择,在没有自定义索引时,自动索引相当于自定义索引;
y.iloc[]利用自动行索引和列索引进行切片或数据选择;
y.ix[]可混合使用自定义索引和自动索引,但不推荐使用;
#行重排
.reindex(index = [])
#列重排
.reindex(column = [])
按索引重新排序
pd.DataFrame.sort_index
sort_index(self, axis=0, level=None, ascending=True, inplace=False, kind=‘quicksort’, na_position=‘last’, sort_remaining=True, by=None)
Sort object by labels (along an axis)
Parameters
----------
axis : index, columns to direct sorting
level : int or level name or list of ints or list of level names
if not None, sort on values in specified index level(s)
ascending : boolean, default True
Sort ascending vs. descending
是否覆盖原数据
inplace : bool, default False
if True, perform operation in-place
kind : {‘quicksort’, ‘mergesort’, ‘heapsort’}, default ‘quicksort’
Choice of sorting algorithm. See also ndarray.np.sort for more
information. mergesort
is the only stable algorithm. For
DataFrames, this option is only applied when sorting on a single
column or label.
na_position : {‘first’, ‘last’}, default ‘last’
first
puts NaNs at the beginning, last
puts NaNs at the end.
Not implemented for MultiIndex.
sort_remaining : bool, default True
if true and sorting by level and index is multilevel, sort by other
levels too (in order) after sorting by specified level
Returns
-------
sorted_obj : DataFrame