.. currentmodule:: pandas .. ipython:: python :suppress: import numpy as np import pandas as pd import os np.random.seed(123456) np.set_printoptions(precision=4, suppress=True) import matplotlib matplotlib.style.use('ggplot') import matplotlib.pyplot as plt pd.options.display.max_rows = 8 dates = pd.date_range('20130101', periods=6) df = pd.DataFrame(np.random.randn(6,4), index=dates, columns=list('ABCD')) Selection --------- .. note:: While standard Python / Numpy expressions for selecting and setting are intuitive and come in handy for interactive work, for production code, we recommend the optimized pandas data access methods, ``.at``, ``.iat``, ``.loc``, ``.iloc`` and ``.ix``. See the indexing documentation :ref:`Indexing and Selecting Data ` and :ref:`MultiIndex / Advanced Indexing ` Getting ~~~~~~~ Selecting a single column, which yields a ``Series``, equivalent to ``df.A`` .. ipython:: python df df['A'] Selecting via ``[]``, which slices the rows. .. ipython:: python df[0:3] df['20130102':'20130104'] Selection by Label ~~~~~~~~~~~~~~~~~~ See more in :ref:`Selection by Label ` For getting a cross section using a label .. ipython:: python df.loc[dates[0]] Selecting on a multi-axis by label .. ipython:: python df.loc[:,['A','B']] Showing label slicing, both endpoints are *included* .. ipython:: python df.loc['20130102':'20130104',['A','B']] Reduction in the dimensions of the returned object .. ipython:: python df.loc['20130102',['A','B']] For getting a scalar value .. ipython:: python df.loc[dates[0],'A'] For getting fast access to a scalar (equiv to the prior method) .. ipython:: python df.at[dates[0],'A'] Selection by Position ~~~~~~~~~~~~~~~~~~~~~ See more in :ref:`Selection by Position ` Select via the position of the passed integers .. ipython:: python df.iloc[3] By integer slices, acting similar to numpy/python .. ipython:: python df.iloc[3:5,0:2] By lists of integer position locations, similar to the numpy/python style .. ipython:: python df.iloc[[1,2,4],[0,2]] For slicing rows explicitly .. ipython:: python df.iloc[1:3,:] For slicing columns explicitly .. ipython:: python df.iloc[:,1:3] For getting a value explicitly .. ipython:: python df.iloc[1,1] For getting fast access to a scalar (equiv to the prior method) .. ipython:: python df.iat[1,1] Boolean Indexing ~~~~~~~~~~~~~~~~ Using a single column's values to select data. .. ipython:: python df[df.A > 0] A ``where`` operation for getting. .. ipython:: python df[df > 0] Using the :func:`~Series.isin` method for filtering: .. ipython:: python df2 = df.copy() df2['E'] = ['one', 'one','two','three','four','three'] df2 df2[df2['E'].isin(['two','four'])] Setting ~~~~~~~ Setting a new column automatically aligns the data by the indexes .. ipython:: python s1 = pd.Series([1,2,3,4,5,6], index=pd.date_range('20130102', periods=6)) s1 df['F'] = s1 Setting values by label .. ipython:: python df.at[dates[0],'A'] = 0 Setting values by position .. ipython:: python df.iat[0,1] = 0 Setting by assigning with a numpy array .. ipython:: python df.loc[:,'D'] = np.array([5] * len(df)) The result of the prior setting operations .. ipython:: python df A ``where`` operation with setting. .. ipython:: python df2 = df.copy() df2[df2 > 0] = -df2 df2