How do I get a DataFrame Index / Series column as an array or list?
Do you know how to get the index column of an dataframe as an array? I have a list of accession numbers in the "Accession" column of a CSV file, which I imported into Pandas, and during the import, I set the index to the "Accession" column. Now, I need the "Accession" column to be a set of labels in a later step, but I don't know how to extract it standalone.
To get a NumPy array, you should use the
In : df = pd.DataFrame(index=['a', 'b']) In : df.index.values Out: array(['a', 'b'], 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.
To get the index as a list, call
In : df.index.tolist() Out: ['a', 'b']
★ Back to homepage or read more recommendations: