How do I get a pandas DataFrame column or index as an array?

Do you know how to get the index or column of a DataFrame as a NumPy array or python list?

To get a NumPy array, you should use the values attribute:

    In [1]: df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]}, index=['a', 'b', 'c']); df
       A  B
    a  1  4
    b  2  5
    c  3  6

    In [2]: df.index.values
    Out[2]: array(['a', 'b', 'c'], dtype=object)

This accesses how the data is already stored, so there's no need for a conversion.
Note: This attribute is also available for many other pandas' objects.

    In [3]: df['A'].values
    Out[3]: Out[16]: array([1, 2, 3])

To get the index as a list, call tolist:

    In [4]: df.index.tolist()
    Out[4]: ['a', 'b']

And similarly, for columns.

From: stackoverflow.com/q/17241004

Back to homepage or read more recommendations: