Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (17.6k points)

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

3 Answers

0 votes
by (41.4k points)

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  B

a  1 4

b  2 5

c  3 6

In [2]: df.index.values

Out[2]: array(['a', 'b', 'c'], dtype=object)

0 votes
by (41.4k points)

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  B

a  1 4

b  2 5

c  3 6

In [2]: df.index.values

Out[2]: array(['a', 'b', 'c'], dtype=object)

0 votes
by (106k points)

You can just do

df.index.values:

df = pd.DataFrame(index=['a', 'b', 'c']) 

df.index.values

Related questions

0 votes
2 answers
0 votes
1 answer
asked Aug 24, 2019 in Data Science by sourav (17.6k points)
0 votes
1 answer
0 votes
1 answer

Browse Categories

...