How to Convert the DataFrame to NumPy Array Without Index in Python [5 Methods]

Do you want to convert the Python dataframe to array without an index? In this Pandas article, I will explain how to convert the dataframe to NumPy array without index in Python using different methods with some examples.

To convert a DataFrame to a NumPy array without the index in Python, you can use several methods: .values to directly access the underlying array, to_numpy() for a flexible approach with type control, iloc[] for selective column conversion, flattening the DataFrame with .ravel() for a 1D array, or list comprehension.

DataFrame to NumPy Array Without Index in Python

There are five different methods to convert the dataframe to NumPy array without index in Python:

  1. Using .values
  2. Using .to_numpy()
  3. Using .iloc
  4. Flattening with .ravel()
  5. Using List Comprehension

Let’s see them one by one using some illustrative examples:

1. Dataframe to NumPy array without index using .values function

The .values method in Python directly accesses the underlying NumPy array of the DataFrame. It’s a quick and easy way to get the array representation but doesn’t offer control over data type conversions.

Here is the code to use the .values to convert the dataframe to NumPy array without index in Python:

import pandas as pd

df_cities = pd.DataFrame({
    'City': ['New York', 'Los Angeles', 'Chicago'],
    'Population': [8419000, 3971000, 2705000]
})
array_cities = df_cities['Population'].values
print("Converted NumPy Array:\n", array_cities)
print("Type of the converted array:", type(array_cities))

Output:

Converted NumPy Array:
 [8419000 3971000 2705000]
Type of the converted array: <class 'numpy.ndarray'>

A screenshot is mentioned below, after implementing the code in the Pycharm editor.

How to Convert the DataFrame to NumPy Array Without Index in Python

2. Pandas to NumPy array without index using to_numpy() function

The to_numpy() function allows us to specify data types and control how NA values are handled, making it ideal for more complex data structures.

This is the way we can use the to_numpy() function to convert the dataframe to NumPy array without index in Python:

import pandas as pd

df_gdp = pd.DataFrame({
    'State': ['California', 'Texas', 'New York'],
    'GDP': [3.1, 1.9, 1.7]
})
array_gdp = df_gdp['GDP'].to_numpy()
print("Converted NumPy Array:\n", array_gdp)
print("Type of the converted array:", type(array_gdp))

Output:

Converted NumPy Array:
 [3.1 1.9 1.7]
Type of the converted array: <class 'numpy.ndarray'>

After executing the code in Pycharm, one can see the output in the below screenshot.

dataframe to numpy array without index in Python

3. Pandas to NumPy without index using iloc[]

The iloc[] method is useful for selecting specific rows and columns before converting to a NumPy array in Python.

Here is the code to convert the dataframe to NumPy array without index in Python using the iloc[] function:

import pandas as pd

df_universities = pd.DataFrame({
    'University': ['MIT', 'Stanford', 'Harvard'],
    'Ranking': [1, 2, 3],
    'Founded': [1861, 1885, 1636]
})
array_universities = df_universities.iloc[:, 0:].values
print("Converted NumPy Array:\n", array_universities)
print("Type of the converted array:", type(array_universities))

Output:

Converted NumPy Array:
 [['MIT' 1 1861]
 ['Stanford' 2 1885]
 ['Harvard' 3 1636]]
Type of the converted array: <class 'numpy.ndarray'>

Below is a screenshot displaying the output following the implementation of the code in the Pycharm editor.

convert to a numpy array before indexing instead in Python

4. Python dataframe to array without index using ravel() function

When we need to convert the DataFrame into a one-dimensional array, this method combines .values with .ravel() to flatten the DataFrame, resulting in a 1D NumPy array.

import pandas as pd

df_capitals = pd.DataFrame({
    'State': ['California', 'Texas', 'Florida'],
    'Capital': ['Sacramento', 'Austin', 'Tallahassee'],
    'Population': [513625, 964254, 195528]
})
array_capitals = df_capitals.values.ravel()
print("Converted NumPy Array:\n", array_capitals)
print("Type of the converted array:", type(array_capitals))

Output:

Converted NumPy Array:
 ['California' 'Sacramento' 513625 'Texas' 'Austin' 964254 'Florida'
 'Tallahassee' 195528]
Type of the converted array: <class 'numpy.ndarray'>

Following the implementation of the code in the Pycharm editor, the screenshot below has been provided.

pandas dataframe to numpy array without index in Python

5. Dataframe to array without index using list comprehension

To convert the dataframe to NumPy array without index in Python, we can iterate over DataFrame rows with a list comprehension and create a list out of it, and then we can convert it using the np.array() function.

import pandas as pd
import numpy as np

df_landmarks = pd.DataFrame({
    'Landmark': ['Statue of Liberty', 'Golden Gate Bridge', 'Grand Canyon'],
    'Location': ['New York', 'California', 'Arizona']
})
array_landmarks = np.array([row for row in df_landmarks.values])
print("Converted NumPy Array:\n", array_landmarks)
print("Type of the converted array:", type(array_landmarks))

Output:

Converted NumPy Array:
 [['Statue of Liberty' 'New York']
 ['Golden Gate Bridge' 'California']
 ['Grand Canyon' 'Arizona']]
Type of the converted array: <class 'numpy.ndarray'>

Below is a screenshot showcasing the output, captured after the code was executed in the Pycharm editor.

pandas to numpy array without index in Python

Conclusion

Here, I have explained five effective methods to convert the dataframe to NumPy array without index in Python: using .values, to_numpy(), iloc[], flattening with .ravel(), and list comprehension. Each method serves different scenarios, ensuring a versatile approach to handling DataFrame-to-array transformations in data analysis workflows.

You may also like to read: