Pandas DataFrame loc []访问一组行和列

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

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值