pandas Using .ix, .iloc, .loc, .at and .iat to access a DataFrame Using .iloc


Example

.iloc uses integers to read and write data to a DataFrame.

First, let's create a DataFrame:

df = pd.DataFrame({'one': [1, 2, 3, 4, 5],
                   'two': [6, 7, 8, 9, 10],
              }, index=['a', 'b', 'c', 'd', 'e'])

This DataFrame looks like:

   one  two
a    1    6
b    2    7
c    3    8
d    4    9
e    5   10

Now we can use .iloc to read and write values. Let's read the first row, first column:

print df.iloc[0, 0]

This will print out:

1

We can also set values. Lets set the second column, second row to something new:

df.iloc[1, 1] = '21'

And then have a look to see what happened:

print df 

   one two
a    1   6
b    2  21
c    3   8
d    4   9
e    5  10