序数索引与DataFrame索引是否有标准区分术语?
问题
我创建了一个DataFrame:
import pandas as pd df = pd.DataFrame({'Name': ['Alice', 'Bob', 'Aritra'], 'Age': [25, 30, 35], 'Location': ['Seattle', 'New York', 'Kona']}, index=([10, 20, 30]))
当让用户输入想要查看的行号时,用户输入0表示第一行,但df.loc[0]无法找到该行,因为DataFrame的index值仅为10、20、30。
我自行定义了“ordinal indexes(序数索引,用于表示行的顺序位置)”和“dataframe indexes(DataFrame索引,即行在DataFrame中的索引值)”来区分这两种索引,且df.index[ordinal_index] == df_index。
请问是否存在区分这两种索引的标准术语?
标准术语区分
在Pandas的官方语境中,这两种索引有明确的标准术语:
- 位置索引(Position-based Index / Integer Position):对应你所说的“序数索引”,指行/列在DataFrame中的顺序位置,从0开始计数。Pandas中用
iloc来基于位置索引访问数据,比如df.iloc[0]就能获取第一行数据。 - 标签索引(Label-based Index):对应你所说的“DataFrame索引”,指DataFrame中显式定义的
index(行标签)或columns(列标签)。Pandas中用loc来基于标签索引访问数据,比如df.loc[10]就能获取索引值为10的行数据。
简单来说,位置索引是“第几行/列”,标签索引是“叫什么名字的行/列”——位置索引由数据排列顺序决定,标签索引则是用户自定义或系统生成的标识值。
内容的提问来源于stack exchange,提问作者Pro Q
相关产品推荐
相关产品推荐

