我是靠谱客的博主 俊逸高跟鞋,最近开发中收集的这篇文章主要介绍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)

Output:

输出:


DataFrame:
Name
ID Role
0
John
1
CEO
1
Jane
2
CTO
2
Mary
3
CFO

1.带有单个标签的loc [] (1. loc[] with a single label)


row_1_series = df.loc[1]
print(type(row_1_series))
print(df.loc[1])

Output:

输出:


<class 'pandas.core.series.Series'>
Name
Jane
ID
2
Role
CTO
Name: 1, dtype: object

2.带有标签列表的loc [] (2. loc[] with a list of label)


row_0_2_df = df.loc[[0, 2]]
print(type(row_0_2_df))
print(row_0_2_df)

Output:

输出:


<class 'pandas.core.frame.DataFrame'>
Name
ID Role
0
John
1
CEO
2
Mary
3
CFO

3.获得单一价值 (3. Getting a Single Value)

We can specify the row and column labels to get the single value from the DataFrame object.

我们可以指定行和列标签以从DataFrame对象获取单个值。


jane_role = df.loc[1, 'Role']
print(jane_role)
# CTO

4.用loc []切片 (4. Slice with loc[])

We can pass a slice of labels too, in that case, the start and stop labels will be included in the result Series object.

我们可以通过一个片标签也是如此,在这种情况下,启动和停止标签将被包括在结果Series对象。


roles = df.loc[0:1, 'Role']
print(roles)

Output:

输出:


0
CEO
1
CTO
Name: Role, dtype: object

5.带有布尔值数组的loc [] (5. loc[] with an array of Boolean values)


row_1_series = df.loc[[False, True, False]]
print(row_1_series)

Output:

输出:


Name
ID Role
1
Jane
2
CTO

Since the DataFrame has 3 rows, the array length should be 3. If the argument boolean array length doesn’t match with the length of the axis, IndexError: Item wrong length is raised.

由于DataFrame具有3行,因此数组长度应为3。如果自变量布尔数组长度与轴的长度不匹配,则会引发IndexError:Item错误的长度

6.带有条件语句的loc [] (6. loc[] with Conditional Statements)


data = df.loc[df['ID'] > 1]
print(data)

Output: A DataFrame of the rows where the ID is greater than 1.

输出 :ID大于1的行的DataFrame。


Name
ID Role
1
Jane
2
CTO
2
Mary
3
CFO

7.具有可调用函数的DataFrame loc [] (7. DataFrame loc[] with Callable Function)

We can also use a lambda function with the DataFrame loc[] attribute.

我们还可以将lambda函数与DataFrame loc []属性一起使用。


id_2_row = df.loc[lambda df1: df1['ID'] == 2]
print(id_2_row)

Output:

输出:


Name
ID Role
1
Jane
2
CTO

使用loc []属性设置DataFrame值 (Setting DataFrame Values using loc[] attribute)

One of the special features of loc[] is that we can use it to set the DataFrame values. Let’s look at some examples to set DataFrame values using the loc[] attribute.

loc []的特殊功能之一是我们可以使用它来设置DataFrame值。 让我们看一些使用loc []属性设置DataFrame值的示例。

1.设置单个值 ( 1. Setting a Single Value)

We can specify the row and column labels to set the value of a specific index.

我们可以指定行标签和列标签来设置特定索引的值。


import pandas as pd
d1 = {'Name': ['John', 'Jane', 'Mary'], 'ID': [1, 2, 3], 'Role': ['CEO', 'CTO', 'CFO']}
df = pd.DataFrame(d1, index=['A', 'B', 'C'])
print('Original DataFrame:n', df)
# set a single value
df.loc['B', 'Role'] = 'Editor'
print('Updated DataFrame:n', df)

Output:

输出:


Original DataFrame:
Name
ID Role
A
John
1
CEO
B
Jane
2
CTO
C
Mary
3
CFO
Updated DataFrame:
Name
ID
Role
A
John
1
CEO
B
Jane
2
Editor
C
Mary
3
CFO

2.设置整行的值 (2. Setting values of an entire row)

If we specify only a single label, all the values in that row will be set to the specified one.

如果仅指定一个标签,则该行中的所有值都将设置为指定的标签。


df.loc['B'] = None
print('Updated DataFrame with None:n', df)

Output:

输出:


Updated DataFrame with None:
Name
ID
Role
A
John
1.0
CEO
B
None
NaN
None
C
Mary
3.0
CFO

3.设置整个列的值 (3. Setting values of an entire column)

We can use a slice to select all the rows and specify a column to set its values to the specified one.

我们可以使用切片来选择所有行,并指定一列以将其值设置为指定的一列。


df.loc[:, 'Role'] = 'Employee'
print('Updated DataFrame Role to Employee:n', df)

Output:

输出:


Updated DataFrame Role to Employee:
Name
ID
Role
A
John
1.0
Employee
B
None
NaN
Employee
C
Mary
3.0
Employee

4.根据条件设定值 (4. Setting Value based on a Condition)


df.loc[df['ID'] == 1, 'Role'] = 'CEO'
print(df)

Output:

输出:


Name
ID
Role
A
John
1.0
CEO
B
None
NaN
Employee
C
Mary
3.0
Employee

结论 (Conclusion)

Python DataFrame loc[] attribute is very useful because we can get specific values as well as set the values. The support for conditional parameters and lambda expressions with the loc[] attribute makes it a very powerful resource.

Python DataFrame loc []属性非常有用,因为我们可以获取特定值并设置值。 loc []属性支持条件参数和lambda表达式,这使其成为非常强大的资源。

参考文献: (References:)

  • DataFrame loc official doc

    DataFrame Loc官方文档

翻译自: https://www.journaldev.com/36384/pandas-dataframe-loc

最后

以上就是俊逸高跟鞋为你收集整理的Pandas DataFrame loc []访问一组行和列的全部内容,希望文章能够帮你解决Pandas DataFrame loc []访问一组行和列所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(53)

评论列表共有 0 条评论

立即
投稿
返回
顶部