I have a DataFrame
pandas and I want to access a specific row and columns, and specifically I want to index the row by position and the column by label. Right now I'm doing it by chaining one call to .iloc
and one call to .loc
, but I'm wondering if there's a more efficient way:
d1 = pd.DataFrame([[1, 2, 3], [4, 5, 6]], columns=["a", "b", "c"])
d1.iloc[-1].loc[:"b"] # Mi método actual
Neither .ix
, nor .loc
nor .iloc
allow me to do it in a single call by indexing rows and columns (I get KeyError
or TypeError
). Am I mistaking something fundamental?
.ix
is built on.loc
e.iloc
. It is primarily based on.loc
but will search by integer as long as the axis is not integer based, in which case it will only search by label. Hence the error.In any case, your current method is more efficient.
http://pandas.pydata.org/pandas-docs/stable/indexing.html#different-choices-for-indexing