ADVERTISEMENT
ADVERTISEMENT

Types of Attributes and Methods of Series

A Series in the Pandas library is a one-dimensional labeled array, essential for dealing with data in Python. Let's look into some key functions of a Pandas Series: axes, dtype, empty, ndim, size, values, head, and tail, along with examples and their expected outputs.

Basic Series Functionality

1. axes: This returns the list of the labels of the series.

# Import Pandas
import pandas as pd

# Create a series
s = pd.Series([1,2,3,4,5], index = ['a','b','c','d','e'])

# Show the axes
print(s.axes)

The output will be: [Index(['a', 'b', 'c', 'd', 'e'], dtype='object')]

2. dtype: This returns the data type of the object in the Series.

# Show the data type
print(s.dtype)

The output will be: int64

 

3. empty: This checks if the series is empty. It returns True if the series is empty, and False otherwise.

# Check if the series is empty
print(s.empty)

The output will be: False

4. ndim: This returns the number of dimensions of the underlying data. For a Series, this is always 1.

# Show the number of dimensions
print(s.ndim)

The output will be: 1 

5. size: This returns the number of elements in the underlying data.

# Show the size
print(s.size)

The output will be: 5

6. values: This returns the Series as ndarray or ndarray-like depending on the dtype.

# Show the values
print(s.values)

The output will be: [1 2 3 4 5]

7. head(n): This returns the first 'n' rows of the series. If 'n' is not specified, it returns the first 5 rows.

# Show the first 3 rows
print(s.head(3))

# output
# a    1
# b    2
# c    3
# dtype: int64

8. tail(n): This returns the last 'n' rows of the series. If 'n' is not specified, it returns the last 5 rows.

# Show the last 2 rows
print(s.tail(2))

# output
# d    4
# e    5
# dtype: int64

These are some of the basic functionalities provided by the Series object in Pandas. By mastering these, you'll have a strong foundation for manipulating and analyzing data efficiently in Python using Pandas.


ADVERTISEMENT

ADVERTISEMENT