pandas 数据框中 .loc 的目的是什么

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37051442/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 01:10:45  来源:igfitidea点击:

What is the purpose of .loc in pandas data frame

pythonrpandasdataframe

提问by Kumar Manglam

I am basically migrating from R to Python. I wanted to subset my data frame based on a column. While going through stack-overflow answer, I found a solution.

我基本上是从 R 迁移到 Python。我想根据列对我的数据框进行子集化。在通过 stack-overflow answer 时,我找到了一个解决方案。

But consider the below code:

但请考虑以下代码:

import pandas as pd
import numpy as np
df = pd.DataFrame({'A': 'foo bar foo bar foo bar foo foo'.split(),
                   'B': 'one one two three two two one three'.split(),
                   'C': np.arange(8), 'D': np.arange(8) * 2})

df1 = df[df['A'] == "foo"]
df1
df2 = df.loc[df['A'] == "foo"]
df2

Both df1 and df2 are same.

df1 和 df2 都是一样的。

So my question is : what is the requirement for locfunction in the first place. Please bear in mind, that I come from R background and in R, we dont have to use loctype function for subsetting data-frame.

所以我的问题是:loc首先对功能的要求是什么。请记住,我来自 R 背景,在 R 中,我们不必使用loc类型函数来设置数据框的子集。

回答by ChristyCasey

I am learning pandas myself, so excuse the answer that isnt particular in depth. The .loc is has a 'location' function allowing you to note a place in the dataframe DF[1,3] in R. Or allowing you to put in two grid coordinates, where otherwise you could have only 1 parameter.

我自己正在学习Pandas,所以请原谅不是特别深入的答案。.loc 有一个“位置”功能,允许您在 R 中记录数据帧 DF[1,3] 中的一个位置。或者允许您输入两个网格坐标,否则您只能有 1 个参数。

Now I could be wrong, as its been a while since I've had a look at pandas, and as I mentioned I am also only learning it.

现在我可能错了,因为我已经有一段时间没有看过Pandas了,正如我所提到的,我也只是在学习它。

It is listed as an indexing function on the website http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.loc.html

它在网站http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.loc.html上列为索引功能

回答by piRSquared

The loc method gives direct access to the dataframe allowing for assignment to specific locations of the dataframe. This is in contrast to the ix method or bracket notation that produces a copy of the requested portion of the dataframe. The consequence is that you cannot make assignments to the dataframe via these methods. The iloc method shares the same characteristic as loc.

loc 方法提供对数据帧的直接访问,允许分配给数据帧的特定位置。这与 ix 方法或括号表示法形成对比,后者生成数据帧请求部分的副本。结果是您无法通过这些方法对数据框进行分配。iloc 方法与 loc 具有相同的特性。