DataFrame attributes
Each of the three DataFrame components–the index, columns, and data–may be accessed from a DataFrame. You might want to perform operations on the individual components and not on the DataFrame as a whole. In general, though we can pull out the data into a NumPy array, unless all the columns are numeric, we usually leave it in a DataFrame. DataFrames are ideal for managing heterogenous columns of data, NumPy arrays not so much.
This recipe pulls out the index, columns, and the data of the DataFrame into their own variables, and then shows how the columns and index are inherited from the same object.
How to do it…
- Use the DataFrame attributes index, columns, and values to assign the index, columns, and data to their own variables:
>>> movies = pd.read_csv("data/movie.csv") >>> columns = movies.columns >>> index = movies.index >>> data = movies.to_numpy()
- Display...