2

Let's say I have an empty pandas dataframe.

import pandas as pd
m = pd.DataFrame(index=range(1,100), columns=range(1,100))
m = m.fillna(0)

What is the difference between the following two commands?

m[2][1]
m[2].ix[1] # This code actually allows you to edit the dataframe

Feel free to provide further reading if it would be helpful for future reference.

zthomas.nc
  • 3,689
  • 8
  • 35
  • 49
  • 2
    related: http://stackoverflow.com/questions/20838395/what-is-the-point-of-ix-indexing-for-pandas-series – John Apr 01 '16 at 04:10
  • 2
    Aside from the above you should look at why the first line may fail and the second one works: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy – EdChum Apr 01 '16 at 07:42

1 Answers1

3

The short answer is that you probably shouldn't do either of these (see @EdChum's link for the reason):

m[2][1]
m[2].ix[1]

You should generally use a single ix, iloc, or loc command on the entire dataframe anytime you want access by both row and column -- not a sequential column access, followed by row access as you did here. For example,

m.iloc[1,2]

Note that the 1 and 2 are reversed compared to your example because ix/iloc/loc all use standard syntax of row then column. Your syntax is reversed because you are chaining, and are first selecting a column from a dataframe (which is a series) and then selecting a row from that series.

In simple cases like yours, it often won't matter too much but the usefulness of ix/iloc/loc is that they are designed to let you "edit" the dataframe in complex ways (aka set or assign values).

There is a really good explanation of ix/iloc/loc here:

pandas iloc vs ix vs loc explanation?

and also in standard pandas documentation:

http://pandas.pydata.org/pandas-docs/stable/indexing.html

Community
  • 1
  • 1
JohnE
  • 29,156
  • 8
  • 79
  • 109