Pandas series is a one-dimensional data structure. It can hold data of many types including objects, floats, strings and integers. You can create a series by calling pandas.Series()
.
An list, numpy array, dict can be turned into a pandas series. You should use the simplest data structure that meets your needs. In this article we’ll discuss the series data structure.
Related course: Data Analysis with Python Pandas
Create series
Introduction
Pandas comes with many data structures for processing data. One of them is a series.
The syntax for a series is:
1 | import pandas as pd |
This creates an empty series.
Create series from list
To turn a list into a series, all you have to do is:
1 | import pandas as pd |
The contents of s is:
0 1
1 2
2 3
3 4
dtype: int64
By default is assigns an index. First it shows the index, then the element value.
Create series from ndarray
You can create a series from a numpy ndarray.
1 | import pandas as pd |
This ouputs the following:
1 | >>> s |
Create a series from a dict
If you have a dictionary, you can turn it into a series:
1 | import pandas as pd |
The contents of the series is as follows:
1 | s |
As index it used the dictionary keys.
Pandas series
Pandas series get index
You can access series data like you would with a list or ndarray.
1 | import pandas as pd |
You slice a series, like you would with a list:
1 | 1,2,3,4,5,6]) data = np.array([ |
1 | 3:5] s[ |
Related course: Data Analysis with Python Pandas