重新修改索引
s = pd.Series([1, 3, 5, 6, 8], index=list('acefh'))
print(s.reindex(list('abcdefgh')))
a 1.0
b NaN
c 3.0
d NaN
e 5.0
f 6.0
g NaN
h 8.0
把默认数字填充为0
print(s.reindex(list('abcdefgh'), fill_value=0))
a 1
b 0
c 3
d 0
e 5
f 6
g 0
h 8
把前面的数值填充到后面的默认值里,method方法只对行有效
print(s.reindex(list('abcdefgh'), method='ffill'))
a 1
b 1
c 3
d 3
e 5
f 6
g 6
h 8
method='bfill’是向上填充
每一列的最大值减去最小值
df = pd.DataFrame(np.arange(12).reshape(4, 3), index=['one', 'two', 'three', 'four'], columns=list('ABC'))
print(df)
print(df.apply(lambda x: x.max()-x.min()))
A B C
one 0 1 2
two 3 4 5
three 6 7 8
four 9 10 11
A 9
B 9
C 9
每一行的最大值减最小值
print(df.apply(lambda x: x.max()-x.min(), axis=1))
one 2
two 2
three 2
four 2
def min_max(x):
return pd.Series([x.min(), x.max()], index=['min', 'max'])
print(df.apply(min_max, axis=1))
min max
one 0 2
two 3 5
three 6 8
four 9 11
只显示小数点后三位
formater = lambda x: '%.03f' % x
print(df.applymap(formater))
A B C
one 0.000 1.000 2.000
two 3.000 4.000 5.000
three 6.000 7.000 8.000
four 9.000 10.000 11.000
根据某一列进行排序
print(df.sort_values(by='one'))
one two three
A 1 5 4
D 6 7 8
C 7 9 7
B 8 7 4
查看这一个元素大小,在某一列或某一行里排名第几
s = pd.Series([3, 6, 2, 6, 4])
0 3
1 6
2 2
3 6
4 4
print(s.rank(method='first'))
第0号位置的元素排第二位,第一号位置的元素排第4位
0 2.0
1 4.0
2 1.0
3 5.0
4 3.0
数据的唯一性
统计每个元素出现的次数
print(s.value_counts())