0

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.

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.

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

Community
  • 1
  • 1
Kumar Manglam
  • 2,780
  • 1
  • 19
  • 28
  • Please read [this](http://pandas.pydata.org/pandas-docs/stable/indexing.html#different-choices-for-indexing) and [this](http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy) if you have further questions then edit your question – EdChum May 05 '16 at 13:12

2 Answers2

1

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.

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.

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

ChristyCasey
  • 426
  • 5
  • 9
1

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.

piRSquared
  • 285,575
  • 57
  • 475
  • 624