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.