pandas 按位置选择
示例
的iloc(短为整数位置)方法允许选择基于它们的位置索引一个数据帧的行。这样,就可以像使用Python的列表切片那样对数据帧进行切片。
df = pd.DataFrame([[11, 22], [33, 44], [55, 66]], index=list("abc")) df # Out: # 0 1 # a 11 22 # b 33 44 # c 55 66 df.iloc[0] # the 0th index (row) # Out: # 0 11 # 1 22 # Name: a, dtype: int64 df.iloc[1] # the 1st index (row) # Out: # 0 33 # 1 44 # Name: b, dtype: int64 df.iloc[:2] # the first 2 rows # 0 1 # a 11 22 # b 33 44 df[::-1] # reverse order of rows # 0 1 # c 55 66 # b 33 44 # a 11 22
行位置可以与列位置组合
df.iloc[:, 1] # the 1st column # Out[15]: # a 22 # b 44 # c 66 # Name: 1, dtype: int64
另请参阅:按位置选择