pandas.DataFrame.getitem

Get data from a DataFrame by indexer.

Limitations

Supported key can be one of the following:

  • String literal, e.g. df['A']

  • A slice, e.g. df[2:5]

  • A tuple of string, e.g. df[('A', 'B')]

  • An array of booleans, e.g. df[True,False]

  • A series of booleans, e.g. df(series([True,False]))

Supported getting a column through getting attribute.

Examples

Getting Pandas DataFrame column through getting attribute.
import pandas as pd
from numba import njit


@njit
def dataframe_getitem():
    df = pd.DataFrame({'A': [0, 1, 2, 3, 4],
                       'B': [1, 2, 3, 4, 5],
                       'C': [2, 3, 4, 5, 6]})

    return df.C


print(dataframe_getitem())
$ python ./dataframe/getitem/df_getitem_attr.py
0    2
1    3
2    4
3    5
4    6
Name: C, dtype: int64
Getting Pandas DataFrame column where key is a string.
import pandas as pd
from numba import njit


@njit
def dataframe_getitem():
    df = pd.DataFrame({'A': [0, 1, 2, 3, 4],
                       'B': [1, 2, 3, 4, 5],
                       'C': [2, 3, 4, 5, 6]})

    return df['A']


print(dataframe_getitem())
$ python ./dataframe/getitem/df_getitem.py
0    0
1    1
2    2
3    3
4    4
Name: A, dtype: int64
Getting slice of Pandas DataFrame.
import pandas as pd
from numba import njit


@njit
def dataframe_getitem():
    df = pd.DataFrame({'A': [0, 1, 2, 3, 4],
                       'B': [1, 2, 3, 4, 5],
                       'C': [2, 3, 4, 5, 6]})

    return df[1:3]


print(dataframe_getitem())
$ python ./dataframe/getitem/df_getitem_slice.py
   A  B  C
1  1  2  3
2  2  3  4
Getting Pandas DataFrame elements where key is a tuple of strings.
import pandas as pd
from numba import njit


@njit
def dataframe_getitem():
    df = pd.DataFrame({'A': [0, 1, 2, 3, 4],
                       'B': [1, 2, 3, 4, 5],
                       'C': [2, 3, 4, 5, 6]})

    return df[('A', 'C')]


print(dataframe_getitem())
$ python ./dataframe/getitem/df_getitem_tuple.py
   A  C
0  0  2
1  1  3
2  2  4
3  3  5
4  4  6
Getting Pandas DataFrame elements where key is an array of booleans.
import pandas as pd
import numpy as np
from numba import njit


@njit
def dataframe_getitem():
    df = pd.DataFrame({'A': [0, 1, 2, 3, 4],
                       'B': [1, 2, 3, 4, 5],
                       'C': [2, 3, 4, 5, 6]})
    arr = np.array([False, True, False, False, True])

    return df[arr]


print(dataframe_getitem())
$ python ./dataframe/getitem/df_getitem_array.py
   A  B  C
1  1  2  3
4  4  5  6
Getting Pandas DataFrame elements where key is series of booleans.
import pandas as pd
from numba import njit


@njit
def dataframe_getitem():
    df = pd.DataFrame({'A': [0, 1, 2, 3, 4],
                       'B': [1, 2, 3, 4, 5],
                       'C': [2, 3, 4, 5, 6]})
    val = pd.Series([True, False, True, False, False])

    return df[val]


print(dataframe_getitem())
$ python ./dataframe/getitem/df_getitem_series.py
   A  B  C
0  0  1  2
2  2  3  4

See also

Series.getitem

Get value(s) of Series by key.

Series.setitem

Set value to Series by index

Series.loc

Access a group of rows and columns by label(s) or a boolean array.

Series.iloc

Purely integer-location based indexing for selection by position.

Series.at

Access a single value for a row/column label pair.

Series.iat

Access a single value for a row/column pair by integer position.

DataFrame.setitem

Set value to DataFrame by index

DataFrame.loc

Access a group of rows and columns by label(s) or a boolean array.

DataFrame.iloc

Purely integer-location based indexing for selection by position.

DataFrame.at

Access a single value for a row/column label pair.

DataFrame.iat

Access a single value for a row/column pair by integer position.