Pandas DataFrame.values


Pandas DataFrame.values

The DataFrame.values property in pandas is used to retrieve the data of a DataFrame as a two-dimensional NumPy array. This property allows you to access the underlying data in a DataFrame for numerical computation or further processing.


Syntax

The syntax for accessing the values property is:

DataFrame.values

Here, DataFrame refers to the pandas DataFrame whose data is being accessed as a NumPy array.


Returns

A two-dimensional numpy.ndarray object containing the values of the DataFrame. If the DataFrame has mixed data types, the array will have the object data type.


Examples

Accessing the Values of a DataFrame

Use the values property to access the data in a DataFrame as a NumPy array.

Python Program

import pandas as pd

# Create a DataFrame
data = {
    'Name': ['Arjun', 'Ram', 'Suresh'],
    'Age': [25, 30, 35],
    'Salary': [70000.5, 80000.0, 90000.0]
}
df = pd.DataFrame(data)

# Access the values of the DataFrame
print("DataFrame Values:")
print(df.values)

Output

DataFrame Values:
[['Arjun' 25 70000.5]
 ['Ram' 30 80000.0]
 ['Suresh' 35 90000.0]]

Accessing a Row of Values

You can use the values property to access a specific row of the DataFrame as a NumPy array.

Python Program

import pandas as pd

# Create a DataFrame
data = {
    'Name': ['Arjun', 'Ram', 'Suresh'],
    'Age': [25, 30, 35],
    'Salary': [70000.5, 80000.0, 90000.0]
}
df = pd.DataFrame(data)

# Access the values of the first row
print("First Row Values:")
print(df.values[0])

Output

First Row Values:
['Arjun' 25 70000.5]

Performing Numerical Operations

The values property can be used for numerical operations on numeric columns of a DataFrame.

Python Program

import pandas as pd

# Create a DataFrame
data = {
    'Name': ['Arjun', 'Ram', 'Suresh'],
    'Age': [25, 30, 35],
    'Salary': [70000.5, 80000.0, 90000.0]
}
df = pd.DataFrame(data)

# Calculate the sum of numeric values in the DataFrame
numeric_values = df[['Age', 'Salary']].values
print("Sum of Numeric Values:")
print(numeric_values.sum())

Output

Sum of Numeric Values:
210090.5

Handling Mixed Data Types

When the DataFrame contains mixed data types, the resulting NumPy array will have the object data type.

Python Program

import pandas as pd

# Create a DataFrame with mixed data types
data = {
    'Name': ['Arjun', 'Ram', 'Suresh'],
    'Age': [25, 30, 35],
    'JoiningDate': pd.to_datetime(['2022-01-01', '2021-05-12', '2020-08-15'])
}
df = pd.DataFrame(data)

# Access the values of the DataFrame
print("DataFrame Values (Mixed Types):")
print(df.values)

Output

DataFrame Values (Mixed Types):
[['Arjun' 25 Timestamp('2022-01-01 00:00:00')]
 ['Ram' 30 Timestamp('2021-05-12 00:00:00')]
 ['Suresh' 35 Timestamp('2020-08-15 00:00:00')]]

Summary

In this tutorial, we explored the DataFrame.values property in pandas. Key points include:

  • Using values to access DataFrame data as a NumPy array
  • Performing numerical operations on numeric data
  • Handling mixed data types in DataFrames

The DataFrame.values property is a convenient way to work with the underlying data in pandas DataFrames.


Python Libraries