Latest Code Tutorials

Pandas values: How to Find DataFrame values in Pandas


Pandas DataFrame values attribute returns a Numpy representation of the given DataFrame. But it is recommended to use DataFrame.to_numpy() instead. The DataFrame values property only returns values in DataFrame, and the axes labels will be removed.

Pandas values: Find DataFrame values in Python

To find the values in DataFrame, use Pandas DataFrame values property. The DataFrame values property returns a Numpy representation of the given DataFrame.

Pandas DataFrame is a two-dimensional size-mutable, potentially heterogeneous tabular data structure with labeled axes (rows and columns).

Arithmetic operations align on both row and column labels. It can be thought of as a dict-like container for Series objects. This is the primary data structure of the Pandas.



Return value

The values property returns an array to represent the Numpy representation of the DataFrame.

Example program on pandas.DataFrame.values

Example 1: Write a program to show the working of pandas.DataFrame.values.

import pandas as pd

df = pd.DataFrame({'Name': ['Rohit', 'Ankit', 'Shivam', 'Shubh', 'Pranav'],
                   'Roll no.': [2, 4, 5, 7, 8],
                   'Marks': [75, 54, 87, 93, 74]})
print(df, "\n")
finaldata = df.values


Name  Roll no.  Marks
0   Rohit         2     75
1   Ankit         4     54
2  Shivam         5     87
3   Shubh         7     93
4  Pranav         8     74

[['Rohit' 2 75]
 ['Ankit' 4 54]
 ['Shivam' 5 87]
 ['Shubh' 7 93]
 ['Pranav' 8 74]]

Here we can see that we have created a DataFrame consisting of the details of a student.

In the next line, we can see that we have printed the Numpy representation of that DataFrame.

Example 2: Write a program to return the numpy representation of multiple Dataframes, and one of the DataFrames must contain values of character Datatype.

See the following code.

import pandas as pd
df1 = pd.DataFrame({'Name': ['Rohit', 'Ankit', 'Shivam', 'Shubh', 'Pranav'],
                    'Weight.': [60, 57, 43, 64, 24],
                    'City': ['Patna', 'Kolkata', 'Delhi', 'Mumbai', 'Jalandhar']})
print(df1, "\n")
finaldata = df1.values
df2 = pd.DataFrame({'Name': ['Rohit', 'Ankit', 'Shivam', 'Alisha', 'Anisha'],
                    'Age': [22, 14, 15, 17, 18],
                    'Gender': ['M', 'M', 'M', 'F', 'F']})
print("\n", df2)
finaldata2 = df2.values
print("\n", finaldata2)
 Name  Weight.       City
0   Rohit       60      Patna
1   Ankit       57    Kolkata
2  Shivam       43      Delhi
3   Shubh       64     Mumbai
4  Pranav       24  Jalandhar

[['Rohit' 60 'Patna']
 ['Ankit' 57 'Kolkata']
 ['Shivam' 43 'Delhi']
 ['Shubh' 64 'Mumbai']
 ['Pranav' 24 'Jalandhar']]

      Name  Age Gender
0   Rohit   22      M
1   Ankit   14      M
2  Shivam   15      M
3  Alisha   17      F
4  Anisha   18      F

 [['Rohit' 22 'M']
 ['Ankit' 14 'M']
 ['Shivam' 15 'M']
 ['Alisha' 17 'F']
 ['Anisha' 18 'F']]

In the above example, we can see that we have created one DataFrame and stored its values(Numpy representation) in the finaldata.

After then, we printed the resultant DataFrame. After that, we created another DataFrame consisting of character Values and printed that DataFrame as well as numpy representation.

See also

Pandas DataFrame apply()

Pandas DataFrame describe()

Pandas DataFrame join()

Pandas DataFrame rank()

Pandas DataFrame merge()

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.