.loc uses labels to read and write data.
Let's setup a DataFrame:
df = pd.DataFrame({'one': [1, 2, 3, 4, 5],
'two': [6, 7, 8, 9, 10],
}, index=['a', 'b', 'c', 'd', 'e'])
Then we can print the DataFrame to have a look at the shape:
print df
This will output
one two
a 1 6
b 2 7
c 3 8
d 4 9
e 5 10
We use the column and row labels to access data with .loc. Let's set row 'c', column 'two' to the value 33:
df.loc['c', 'two'] = 33
This is what the DataFrame now looks like:
one two
a 1 6
b 2 7
c 3 33
d 4 9
e 5 10
Of note, using df['two'].loc['c'] = 33
may not report a warning, and may even work, however, using df.loc['c', 'two']
is guaranteed to work correctly, while the former is not.
We can read slices of data, for example
print df.loc['a':'c']
will print rows a to c. This is inclusive.
one two
a 1 6
b 2 7
c 3 8
And finally, we can do both together:
print df.loc['b':'d', 'two']
Will output rows b to c of column 'two'. Notice that the column label is not printed.
b 7
c 8
d 9
If .loc is supplied with an integer argument that is not a label it reverts to integer indexing of axes (the behaviour of .iloc). This makes mixed label and integer indexing possible:
df.loc['b', 1]
will return the value in 2nd column (index starting at 0) in row 'b':
7