Python Pandas Series

The Pandas Series can be defined as a one-dimensional array that is capable of storing various data types. We can easily convert the list, tuple, and dictionary into series using "series' method. The row labels of series are called the index. A Series cannot contain multiple columns. It has the following parameter:

  • data: It can be any list, dictionary, or scalar value.
  • index: The value of the index should be unique and hashable. It must be of the same length as data. If we do not pass any index, default np.arrange(n) will be used.
  • dtype: It refers to the data type of series.
  • copy: It is used for copying the data.

Creating a Series:

We can create a Series in two ways:

  1. Create an empty Series
  2. Create a Series using inputs.

Create an Empty Series:

We can easily create an empty series in Pandas which means it will not have any value.

The syntax that is used for creating an Empty Series:

The below example creates an Empty Series type object that has no values and having default datatype, i.e., float64.

Example

Output

Series([], dtype: float64)

Creating a Series using inputs:

We can create Series by using various inputs:

  • Array
  • Dict
  • Scalar value

Creating Series from Array:

Before creating a Series, firstly, we have to import the numpy module and then use array() function in the program. If the data is ndarray, then the passed index must be of the same length.

If we do not pass an index, then by default index of range(n) is being passed where n defines the length of an array, i.e., [0,1,2,....range(len(array))-1].

Example

Output

0    P
1    a
2    n
3    d
4    a
5    s
dtype: object

Create a Series from dict

We can also create a Series from dict. If the dictionary object is being passed as an input and the index is not specified, then the dictionary keys are taken in a sorted order to construct the index.

If index is passed, then values correspond to a particular label in the index will be extracted from the dictionary.

Output

x     0.0
y     1.0
z     2.0
dtype: float64

Create a Series using Scalar:

If we take the scalar values, then the index must be provided. The scalar value will be repeated for matching the length of the index.

Output

0      4
1      4
2      4
3      4
dtype: int64

Accessing data from series with Position:

Once you create the Series type object, you can access its indexes, data, and even individual elements.

The data in the Series can be accessed similar to that in the ndarray.

Output

1 

Series object attributes

The Series attribute is defined as any information related to the Series object such as size, datatype. etc. Below are some of the attributes that you can use to get the information about the Series object:

AttributesDescription
Series.indexDefines the index of the Series.
Series.shapeIt returns a tuple of shape of the data.
Series.dtypeIt returns the data type of the data.
Series.sizeIt returns the size of the data.
Series.emptyIt returns True if Series object is empty, otherwise returns false.
Series.hasnansIt returns True if there are any NaN values, otherwise returns false.
Series.nbytesIt returns the number of bytes in the data.
Series.ndimIt returns the number of dimensions in the data.
Series.itemsizeIt returns the size of the datatype of item.

Retrieving Index array and data array of a series object

We can retrieve the index array and data array of an existing Series object by using the attributes index and values.

Output

RangeIndex(start=0, stop=4, step=1)
[2 4 6 8]
Index(['a', 'b', 'c'], dtype='object')
[11.2 18.6 22.5]

Retrieving Types (dtype) and Size of Type (itemsize)

You can use attribute dtype with Series object as <objectname> dtype for retrieving the data type of an individual element of a series object, you can use the itemsize attribute to show the number of bytes allocated to each data item.

Output

int64
8
float64
8

Retrieving Shape

The shape of the Series object defines total number of elements including missing or empty values(NaN).

Output

(4,)
(3,)

Retrieving Dimension, Size and Number of bytes:

Output

1 1
4 3
32 24

Checking Emptiness and Presence of NaNs

To check the Series object is empty, you can use the empty attribute. Similarly, to check if a series object contains some NaN values or not, you can use the hasans attribute.

Example

Output

False   False   True
True    False   False
4   3
3   3

Series Functions

There are some functions used in Series which are as follows:

FunctionsDescription
Pandas Series.map()Map the values from two series that have a common column.
Pandas Series.std()Calculate the standard deviation of the given set of numbers, DataFrame, column, and rows.
Pandas Series.to_frame()Convert the series object to the dataframe.
Pandas Series.value_counts()Returns a Series that contain counts of unique values.





Latest Courses