🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEpandas

pandas Documentation

LOADING ENGINE...

df.values

AI & DATA SCIENCE // df-values

df.values returns the DataFrame's underlying data as a plain 2D NumPy array, discarding the index and column labels.

Syntax

df.values

Deep Dive Course

Converting a DataFrame to .values strips away all the labeled, pandas-specific structure and gives you the raw data as a NumPy array — useful when passing data into a library, like scikit-learn, that expects a plain array rather than a DataFrame. If the DataFrame's columns have mixed dtypes, some int, some float, some object, the resulting array is upcast to a single common dtype, typically object, which can silently lose the performance benefits of NumPy's native numeric types.

1Understanding df.values

Converting a DataFrame to .values strips away all the labeled, pandas-specific structure and gives you the raw data as a NumPy array — useful when passing data into a library, like scikit-learn, that expects a plain array rather than a DataFrame. If the DataFrame's columns have mixed dtypes, some int, some float, some object, the resulting array is upcast to a single common dtype, typically object, which can silently lose the performance benefits of NumPy's native numeric types.

💡

Prefer df.to_numpy() over the older df.values for converting to a NumPy array in new code — to_numpy() is the modern, explicitly recommended method with clearer, more consistent behavior across pandas versions, though both currently do the same thing for most everyday use cases.

editor.html
import pandas as pd

df = pd.DataFrame({"a": [1, 2], "b": [3, 4]})
print(df.values)
print(type(df.values))
localhost:3000

2Practical Example

Here is a real-world application of df.values showing how it is used in production Pandas code.

editor.html
import pandas as pd

df = pd.DataFrame({"a": [1, 2], "b": [1.5, 2.5]})
arr = df.values
print(arr.dtype)
localhost:3000

3Best Practices

Follow these guidelines when working with df.values:

1. Use df.to_numpy() instead of df.values in new code, since it's the currently recommended, more explicit method for the same conversion

2. Check the resulting array's dtype after converting a mixed-dtype DataFrame, since it commonly upcasts to a less specific type like object

3. Convert to a NumPy array only when a downstream tool specifically requires one — stay in DataFrame form otherwise to keep column labels and pandas' convenience methods available

⚠️

Tip: Prefer df.to_numpy() over the older df.values for converting to a NumPy array in new code — to_numpy() is the modern, explicitly recommended method with clearer, more consistent behavior across pandas versions, though both currently do the same thing for most everyday use cases.

editor.html
import pandas as pd

df = pd.DataFrame({"a": [1, 2], "b": [3, 4]})
print(df.values)
print(type(df.values))
localhost:3000

Examples

Example 01Basic Usage
import pandas as pd

df = pd.DataFrame({"a": [1, 2], "b": [3, 4]})
print(df.values)
print(type(df.values))
Example 02Advanced Example
import pandas as pd

df = pd.DataFrame({"a": [1, 2], "b": [1.5, 2.5]})
arr = df.values
print(arr.dtype)

Best Practices

  • Use df.to_numpy() instead of df.values in new code, since it's the currently recommended, more explicit method for the same conversion
  • Check the resulting array's dtype after converting a mixed-dtype DataFrame, since it commonly upcasts to a less specific type like object
  • Convert to a NumPy array only when a downstream tool specifically requires one — stay in DataFrame form otherwise to keep column labels and pandas' convenience methods available

Interview Question

Why does converting a DataFrame with one int column and one float column to .values produce an array with dtype float64, rather than keeping each column's original dtype?

Hint: Think about the requirement that a single NumPy array can only have one dtype.

A single NumPy array can only hold one shared dtype for every element, unlike a DataFrame, where each column is free to have its own separate dtype. When converting to a 2D array, pandas has to pick one common dtype that can represent every value across all the original columns without losing information, and since a float can represent any integer's value without loss, it upcasts the whole result to float64 — the same 'most general type wins' promotion rule that applies to combining arrays of different dtypes in NumPy directly.

Exercises

MediumPractice using df.values in a real scenario.
View Solution
import pandas as pd

df = pd.DataFrame({"a": [1, 2], "b": [3, 4]})
print(df.values)
print(type(df.values))

Frequently Asked Questions

Why does converting a DataFrame with one int column and one float column to .values produce an array with dtype float64, rather than keeping each column's original dtype?

A single NumPy array can only hold one shared dtype for every element, unlike a DataFrame, where each column is free to have its own separate dtype. When converting to a 2D array, pandas has to pick one common dtype that can represent every value across all the original columns without losing information, and since a float can represent any integer's value without loss, it upcasts the whole result to float64 — the same 'most general type wins' promotion rule that applies to combining arrays of different dtypes in NumPy directly.

Related Functions

ndarray-dtypedf-astypenp-array