Change language

Python | Pandas Series.as_matrix ()

|

Series.as_matrix() Pandas Series.as_matrix() is used to transform a given series or frame object data into a Numpy array view.

Syntax: Series.as_matrix (columns = None)

Parameter:
columns: If None, return all columns, otherwise, returns specified columns.

Returns: values: ndarray

Example # 1: Use Series.as_matrix () to return an array representation - Series.as_matrix () of a given series object.

# import pandas as pd

import pandas as pd

 
# Create a series

sr = pd.Series ([ ’New York’ , ’ Chicago’ , ’Toronto’ , ’ Lisbon’ , ’Rio’ ])

 
# Create index

index_ = [ ’City 1’ , ’City 2’ , ’ City 3’ , ’City 4’ , ’ City 5’

  
# set index

sr.index = index_

 
# Print series

print (sr)

Exit:

 City 1 New York City 2 Chicago City 3 Toronto City 4 Lisbon City 5 Rio dtype: object 

We will now use Series.as_matrix () to return an array representation for a given series object.

# return a massive view

result = sr.as_matrix ()

 
# Print result

print (result)

Output:

 [’New York’ ’Chicago’’ Toronto’ ’Lisbon’’ Rio’] 

As we can see in the output, Series.as_matrix () has successfully returned an array representation for this series object.

Example # 2: Use Series.as_matrix () to return an array representation - Series.as_matrix () of a given series object.

# import pandas as pd

import pandas as pd

 
# Create a series

sr = pd.Series ([  11 , 21 , 8 , 18 , 65 , 18 , 32 , 10 , 5 , 32 , None ])

 
# Create index
# apply annual rate

index_ = pd.date_range ( ’ 2010-10-09 08:45’ , periods   = 11 , freq = ’Y’ )

  
# set index

sr.index = index_

 
# Print series

print (sr)

Output:

 2010-12-31 08:45 : 00 11.0 2011-12-31 08:45:00 21.0 2012-12-31 08:45:00 8.0 2013-12-31 08:45:00 18.0 2014-12-31 08:45:00 65.0 2015-12 -31 08:45:00 18.0 2016-12-31 08:45:00 32.0 2017-12-31 08:45:00 10.0 2018-12-31 08:45:00 5.0 2019-12-31 08:45: 00 32.0 2020-12-31 08:45:00 NaN Freq: A-DEC, dtype: float64 

Now we will use Se ries.as_matrix () to return an array representation for a given series object.

# return a massive view

result = sr.as_matrix ()

 
# Print result

print (result)

Output:

 [11. 21. 8. 18. 65. 18. 32. 10. 5. 32. nan] 

Like us we can see in the output, Series.as_matrix () has successfully returned an array representation for the given series object.

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically