pandas.Series.loc#
- property Series.loc[源代码]#
通过标签或布尔数组访问一组行和列。
pandas.DataFrame.le
允许的输入包括:
pandas.DataFrame.loc
.loc[]主要基于标签,但也可与布尔数组一起使用。单个标签,例如
5或'a'(请注意,5被解释为索引的*标签*,**绝不**被解释为沿索引的整数位置)。警告
标签列表或数组,例如
['a', 'b', 'c']。一个与被切片的轴长度相同的布尔数组,例如
[True, False, True]。一个可对齐的布尔 Series。键的索引将在遮蔽前对齐。
一个可对齐的 Index。返回的选择的 Index 将是输入。
一个
callable函数,带有一个参数(调用它的 Series 或 DataFrame),并返回有效的索引输出(上述之一)。
更多信息请参阅 Selection by Label 。
- Raises:
- KeyError
如果任何项未找到。
- IndexingError
如果传递了一个被索引的键,并且该键的索引与框架索引不可对齐。
参见
DataFrame.at按行/列标签对访问单个值。
DataFrame.iloc通过整数位置访问行和列的组。
DataFrame.xs从 Series/DataFrame 返回一个横截面(一行或多行或一列或多列)。
Series.loc使用标签访问值的组。
Examples
获取值
>>> df = pd.DataFrame([[1, 2], [4, 5], [7, 8]], ... index=['cobra', 'viper', 'sidewinder'], ... columns=['max_speed', 'shield']) >>> df max_speed shield cobra 1 2 viper 4 5 sidewinder 7 8
单个标签。注意,这将返回作为 Series 的行。
>>> df.loc['viper'] max_speed 4 shield 5 Name: viper, dtype: int64
标签列表。注意,使用
[[]]返回一个 DataFrame。>>> df.loc[['viper', 'sidewinder']] max_speed shield viper 4 5 sidewinder 7 8
行的单个标签和列的单个标签。
>>> df.loc['cobra', 'shield'] 2
对行使用标签切片,对列使用单个标签。如上所述,请注意切片的开始和结束都包含在内。
>>> df.loc['cobra':'viper', 'max_speed'] cobra 1 viper 4 Name: max_speed, dtype: int64
与行轴长度相同的布尔列表。
>>> df.loc[[False, False, True]] max_speed shield sidewinder 7 8
可对齐的布尔 Series:
>>> df.loc[pd.Series([False, True, False], ... index=['viper', 'sidewinder', 'cobra'])] max_speed shield sidewinder 7 8
Index(行为与
df.reindex相同)。>>> df.loc[pd.Index(["cobra", "viper"], name="foo")] max_speed shield foo cobra 1 2 viper 4 5
返回布尔 Series 的条件。
>>> df.loc[df['shield'] > 6] max_speed shield sidewinder 7 8
返回带有指定列标签的布尔 Series 的条件。
>>> df.loc[df['shield'] > 6, ['max_speed']] max_speed sidewinder 7
使用
&的多个条件,返回布尔 Series。>>> df.loc[(df['max_speed'] > 1) & (df['shield'] < 8)] max_speed shield viper 4 5
使用
|的多个条件,返回布尔 Series。>>> df.loc[(df['max_speed'] > 4) | (df['shield'] < 5)] max_speed shield cobra 1 2 sidewinder 7 8
请确保每个条件都用括号
()包裹。有关布尔索引的更多详细信息和解释,请参阅 user guide 。备注
如果您发现自己在
.loc[]中使用 3 个或更多条件,请考虑使用 advanced indexing 。下面是如何在具有 MultiIndex 的 DataFrame 上使用
.loc[]的示例。返回布尔 Series 的可调用函数。
>>> df.loc[lambda df: df['shield'] == 8] max_speed shield sidewinder 7 8
设置值
为匹配标签列表的所有项设置值。
>>> df.loc[['viper', 'sidewinder'], ['shield']] = 50 >>> df max_speed shield cobra 1 2 viper 4 50 sidewinder 7 50
为整行设置值。
>>> df.loc['cobra'] = 10 >>> df max_speed shield cobra 10 10 viper 4 50 sidewinder 7 50
为整列设置值。
>>> df.loc[:, 'max_speed'] = 30 >>> df max_speed shield cobra 30 10 viper 30 50 sidewinder 30 50
为匹配可调用条件的行设置值。
>>> df.loc[df['shield'] > 35] = 0 >>> df max_speed shield cobra 30 10 viper 0 0 sidewinder 0 0
将值添加到匹配的位置。
>>> df.loc["viper", "shield"] += 5 >>> df max_speed shield cobra 30 10 viper 0 5 sidewinder 0 0
使用
Series或DataFrame进行设置会根据索引标签设置值,而不是索引位置。>>> shuffled_df = df.loc[["viper", "cobra", "sidewinder"]] >>> df.loc[:] += shuffled_df >>> df max_speed shield cobra 60 20 viper 0 10 sidewinder 0 0
获取具有整数标签索引的 DataFrame 的值
另一个使用整数作为索引的示例。
>>> df = pd.DataFrame([[1, 2], [4, 5], [7, 8]], ... index=[7, 8, 9], columns=['max_speed', 'shield']) >>> df max_speed shield 7 1 2 8 4 5 9 7 8
对行使用整数标签切片。如上所述,请注意切片的开始和结束都包含在内。
>>> df.loc[7:9] max_speed shield 7 1 2 8 4 5 9 7 8
使用 MultiIndex 获取值
一些使用具有 MultiIndex 的 DataFrame 的示例。
>>> tuples = [ ... ('cobra', 'mark i'), ('cobra', 'mark ii'), ... ('sidewinder', 'mark i'), ('sidewinder', 'mark ii'), ... ('viper', 'mark ii'), ('viper', 'mark iii') ... ] >>> index = pd.MultiIndex.from_tuples(tuples) >>> values = [[12, 2], [0, 4], [10, 20], ... [1, 4], [7, 1], [16, 36]] >>> df = pd.DataFrame(values, columns=['max_speed', 'shield'], index=index) >>> df max_speed shield cobra mark i 12 2 mark ii 0 4 sidewinder mark i 10 20 mark ii 1 4 viper mark ii 7 1 mark iii 16 36
单个标签。注意,这将返回一个具有单一索引的 DataFrame。
>>> df.loc['cobra'] max_speed shield mark i 12 2 mark ii 0 4
单个索引元组。注意,这将返回一个 Series。
>>> df.loc[('cobra', 'mark ii')] max_speed 0 shield 4 Name: (cobra, mark ii), dtype: int64
行的单个标签和列的单个标签。与传入元组类似,这将返回一个 Series。
>>> df.loc['cobra', 'mark i'] max_speed 12 shield 2 Name: (cobra, mark i), dtype: int64
单个元组。注意,使用
[[]]返回一个 DataFrame。>>> df.loc[[('cobra', 'mark ii')]] max_speed shield cobra mark ii 0 4
索引的单个元组和列的单个标签。
>>> df.loc[('cobra', 'mark i'), 'shield'] 2
从索引元组到单个标签的切片。
>>> df.loc[('cobra', 'mark i'):'viper'] max_speed shield cobra mark i 12 2 mark ii 0 4 sidewinder mark i 10 20 mark ii 1 4 viper mark ii 7 1 mark iii 16 36
从索引元组到索引元组的切片。
>>> df.loc[('cobra', 'mark i'):('viper', 'mark ii')] max_speed shield cobra mark i 12 2 mark ii 0 4 sidewinder mark i 10 20 mark ii 1 4 viper mark ii 7 1
有关高级索引的更多详细信息和解释,请参阅 user guide 。