What does the pandas.series.values attribute do?

PandasServer Side ProgrammingProgramming

A pandas series object is used to store 1-dimensional labeled data, that data is called values and the labels are called indexes in pandas.

In pandas data structures we can store any kind of data like text data, integer values, and time sequence, and more. We can access series elements by using the respected labels. instead of accessing elements by labels, we can get all elements in a ndarray type object.

Example1

import pandas as pd

# creating a series
s = pd.Series([10,10,20,30,40])
print(s)

# Getting values
values = s.values

print('Output: ')

# displaying outputs
print(values)

Explanation

Initially, we have created a pandas Series object with a list of integer values. And The “.values” attribute applied to the series object then will return a ndarray with series values.

Output

0 10
1 10
2 20
3 30
4 40
dtype: int64

Output:
[10 10 20 30 40]

The “.values” attribute accesses the values in the serial object and returns a ndarray object with values. Those two objects are displayed in the above output block.

Example 2

import pandas as pd

s = pd.Series({97:'a', 98:'b', 99:'c', 100:'d', 101:'e', 102:'f'})
print(s)

# Getting values
values = s.values

print('Output: ')

# displaying outputs
print(values)

Explanation

In the following example, we have created another pandas series object with string data. For that we have initialized a python dictionary then applied it to the pandas.Series constructor.

Here our goal is to apply the ".values” attribute to see the result output object to see the values in the given pandas series.

Output

97  a
98  b
99  c
100 d
101 e
102 f
dtype: object

Output:
['a' 'b' 'c' 'd' 'e' 'f']

The s.values attribute will return a ndarray of all values in given series, for the following example the given series object “s” is only having string type data.

Apply type() function to verify which type of python object is returned as an output for this .values attribute.

raja
Updated on 09-Mar-2022 06:03:31

Advertisements