Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

How are iloc and loc different?

df = pd.DataFrame({'col1': [1,2,3,4,5], 'col2': ["foo", "bar", "baz", "foobar", "foobaz"]})
  col1  col2
0   1   foo
1   2   bar
2   3   baz
3   4   foobar
4   5   foobaz

df = df.sort_values('col1', ascending = False)
      col1  col2
    4   5   foobaz
    3   4   foobar
    2   3   baz
    1   2   bar
    0   1   foo
    
# value at index location (iloc)
df.iloc[0, 0:2]
col1         5
col2    foobaz
Name: 4, dtype: object
# value at index label (loc)
df.loc[0, 'col1':'col2']
col1      1
col2    foo
Name: 0, dtype: object

Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #How #iloc #loc
ADD COMMENT
Topic
Name
2+7 =