Pandas DataFrame loc[] allows us to access a group of rows and columns. We can pass labels as well as boolean values to select the rows and columns.
Pandas DataFrame loc []允许我们访问一组行和列。 我们可以传递标签以及布尔值来选择行和列。
DataFrame loc []输入 (DataFrame loc[] inputs)
Some of the allowed inputs are:
一些允许的输入是:
- A Single Label – returning the row as Series object. 单个标签 –将行作为Series对象返回。
- A list of Labels – returns a DataFrame of selected rows. 标签列表 –返回选定行的DataFrame。
- A Slice with Labels – returns a Series with the specified rows, including start and stop labels. 带标签的切片 –返回具有指定行的系列,包括开始和结束标签。
- A boolean array – returns a DataFrame for True labels, the length of the array must be the same as the axis being selected. 布尔数组 –返回带有True标签的DataFrame,该数组的长度必须与所选轴相同。
- A conditional statement or callable function – must return a valid value to select the rows and columns to return. 条件语句或可调用函数 –必须返回有效值以选择要返回的行和列。
DataFrame loc []示例 (DataFrame loc[] Examples)
Let’s look into some examples of using the loc attribute of the DataFrame object. But, first, we will create a sample DataFrame for us to use.
让我们看一些使用DataFrame对象的loc属性的示例。 但是,首先,我们将创建一个示例数据框架供我们使用。
import pandas as pd
d1 = {'Name': ['John', 'Jane', 'Mary'], 'ID': [1, 2, 3], 'Role': ['CEO', 'CTO', 'CFO']}
df = pd.DataFrame(d1)
print('DataFrame:\n', df)
Outpu