Pandas DataFrame values property with examples

Spread the love

The values property in pandas returns the numpy representation of the dataframe. Only the values in the DataFrame will be returned and the axes labels will be removed.

Syntax –

dataframe.values

Examples –

1 . All the columns are of same type –

When all the columns are of same type (e.g., int64) results in an array of the same type.

Let’s create a dataframe.

import pandas as pd

data = {'Apple':[89, 89, 90, 110, 125, 84, 131, 123, 123, 140, 145, 145],
       'Orange': [46, 46, 50, 65, 63, 48, 110, 120, 60, 42, 47, 62],
       'Banana': [26, 30, 30, 25, 38, 22, 22, 36, 20, 27, 23, 34 ],
       'Mango': [80, 80, 90, 125, 130, 150, 140, 140, 135, 135, 80, 90]}

index = ['Jan','Feb','Mar','Apr','May','June','Jul','Aug','Sep','Oct','Nov','Dec']
df = pd.DataFrame(data, index=index)
df

Now, we can use the values property to get the numpy representation of this dataframe.

df.values

2. Columns are Mixed Type –

A DataFrame with mixed type columns(e.g., str/object, int64, float32) results in an ndarray of the broadest type that accommodates these mixed types (e.g., object).

Let’s create a dataframe of mixed types.

import pandas as pd

data = {'Name': ['Eleven','Mike','Lucas','Will','Max'],
       'Age': [18, 20, 20, 18, 19],
       'Sex': ['F', 'M', 'M', 'M', 'F'],
       'Marks': [99, 85, 82, 70, 80]}
df = pd.DataFrame(data)
df

Now, let’s use the values property.

df.values

Rating: 1 out of 5.

Leave a Reply