1. 先看一个典型的例子:
alist = [ 3,5,8,0,-9,np.nan,18]
sers = pd.Series(alist) # 测试一
print(sers)
sers.sort_values(axis=0, ascending=False, inplace=True) # 降序
sers.dropna(inplace=True)
print("排序后:")
print(sers)
print(type(sers))
print(f" index[0]:{sers.index[0]}")
print(f" [0]:{sers[0]} iloc[0]:{sers.iloc[0]}")
print(f" index[-1]:{sers.index[-1]}")
print(f" iloc[-1]:{sers.iloc[-1]}")
运行结果是:
0 3.0
1 5.0
2 8.0
3 0.0
4 -9.0
5 NaN
6 18.0
dtype: float64
排序后:
6 18.0
2 8.0
1 5.0
0 3.0
3 0.0
4 -9.0
dtype: float64
<class 'pandas.core.series.Series'>
index[0]:6
[0]:3.0 iloc[0]:18.0
index[-1]:4
iloc[-1]:-9.0
可以看到,sers[0]和sers.iloc[0]是不一样的,前