Back
Do you know how to get the index or column of a DataFrame as a NumPy array or python list?
Use the values attribute to get a NumPy array:
In [1]: df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]}, index=['a', 'b', 'c']); df A Ba 1 4b 2 5c 3 6In [2]: df.index.valuesOut[2]: array(['a', 'b', 'c'], dtype=object)
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)
You can just do
df.index.values:df = pd.DataFrame(index=['a', 'b', 'c']) df.index.values
df.index.values:
df = pd.DataFrame(index=['a', 'b', 'c'])
df.index.values
31k questions
32.8k answers
501 comments
693 users