Python Pandas Series (original) (raw)
Last Updated : 10 May, 2025
Pandas **Series is a one-dimensional labeled array that can hold data of any type (integer, float, string, Python objects, etc.). It is similar to a column in an Excel spreadsheet or a database table. In this article we will study Pandas Series a powerful one-dimensional data structure in Python.
**Key Features of Pandas Series:
- Supports integer-based and label-based indexing.
- Stores heterogeneous data types.
- Offers a variety of built-in methods for data manipulation and analysis.
Creating a Pandas Series
A **Pandas Series can be created from different data structures such as lists, NumPy arrays, dictionaries or scalar value.
Python `
import pandas as pd
data = [1, 2, 3, 4]
ser = pd.Series(data) print(ser)
`
**Output:
The numbers on the left (0, 1, 2, 3) represent the **index which is automatically assigned by Pandas. The values (10, 20, 30, 40) represent the **data stored in the Series
For a deeper dive into creating Pandas Series check out our detailed guide: **Creating a Pandas Series Explained.
Accessing element of Series
In Pandas we can access element of series using two ways:
- **Position-based Indexing - In this we use numerical positions similar to lists in Python.
- **Label-based Indexing - This method also custom index labels assigned to elements.
**Position-based Indexing
In order to access the series element refers to the index number. Use the index operator []to access an element in a series. The index must be an integer. In order to access multiple elements from a series we use Slice operation.
Python `
import pandas as pd import numpy as np
data = np.array(['g','e','e','k','s','f', 'o','r','g','e','e','k','s']) ser = pd.Series(data)
print(ser[:5])
`
**Output:
This prints the first five elements of the Series. The index is the default integer index starting from 0. So ser[:5] returns values from index 0 to 4 i.e., 5 elements in total.
**Label-based Indexing
In order to access an element from series, we have to set values by index label. A Series is like a fixed-size dictionary in that you can get and set values by index label. let's see a example to understand this
Python `
import pandas as pd import numpy as np
data = np.array(['g','e','e','k','s','f', 'o','r','g','e','e','k','s']) ser = pd.Series(data,index=[10,11,12,13,14,15,16,17,18,19,20,21,22])
print(ser[16])
`
**Output:
The value at index label 16
is "o"
because in our custom index label 16
corresponds to the 7th item in the array data
which is 'o'
For more details refer to Accessing element of Series
Indexing and Selecting Data in Series
Indexing in pandas means simply selecting particular data from a Series. Indexing could mean selecting all the data some of the data from particular columns. Indexing can also be known as **Subset Selection. You can use .iloc[] for **position-based selection and .loc[] for **label-based selection.
**Indexing a Series using .loc[]
This function selects data by refering the explicit index . The df.loc indexer selects data in a different way than just the indexing operator. It can select subsets of data.
Python `
import pandas as pd
df = pd.read_csv("nba.csv")
ser = pd.Series(df['Name']) data = ser.head(10) data
`
**Output:
Now we access the element of series using .loc[] function.
Python `
data.loc[3:6]
`
**Output :
We used data.loc[3:6] which retrieves elements with labels from 3 to 6. Since loc[] includes both start and end labels, it returns names at positions 3, 4, 5 and 6 from the top 10 rows of the "Name" column.
**Indexing a Series using .iloc[]
This function allows us to retrieve data by position. In order to do that we’ll need to specify the positions of the data that we want. The df.iloc indexer is very similar to df.loc but only uses integer locations to make its selections.
Python `
import pandas as pd
df = pd.read_csv("nba.csv")
ser = pd.Series(df['Name']) data = ser.head(10) data
`
**Output:
Now we access the element of Series using .iloc[] function.
Python `
data.iloc[3:6]
`
**Output :
Here, we used ilo
c [3:6] which works on position-based indexing like Python lists. It fetch elements at index positions 3 to 5 excluding index 6.
To understand Indexing and Selecting the data in more detail click on Indexing in Pandas
Binary Operations on Pandas Series
Pandas allows performing **binary operations on Series, such as addition, subtraction, multiplication, and division. These operations can be performed using functions like .add() , .sub(), .mul() and .div().
**Example: Performing Binary Operations
Python `
import pandas as pd
ser1 = pd.Series([1, 2, 3], index=['A', 'B', 'C']) ser2 = pd.Series([4, 5, 6], index=['A', 'B', 'C'])
df_sum = ser1.add(ser2) print(df_sum)
`
**Output:
Each element of ser1 is added to the corresponding element of ser2 by matching the index labels (A,B,C).
**Common Binary Operations
sub() | Method is used to subtract series or list like objects with same length from the caller series |
---|---|
mul() | Method is used to multiply series or list like objects with same length with the caller series |
div() | Method is used to divide series or list like objects with same length by the caller series |
sum() | Returns the sum of the values for the requested axis |
prod() | Returns the product of the values for the requested axis |
mean() | Returns the mean of the values for the requested axis |
pow() | Method is used to put each element of passed series as exponential power of caller series and returned the results |
abs() | Method is used to get the absolute numeric value of each element in Series/DataFrame |
cov() | Method is used to find covariance of two series |
For a deeper understanding of binary operations, refer to: Binary operation methods on series
Conversion Operation on Series
Conversion operations allow transforming data types within a Series. This can be useful for ensuring consistency in data types. In order to perform conversion operation we have various function which help in conversion like .astype(), .tolist() etc
Python `
import pandas as pd
ser = pd.Series([1, 2, 3, 4]) ser = ser.astype(float) print(ser)
`
**Output:
The .astype(float) method converts all integer values in the Series to float. So 1 becomes 1.0, 2 becomes 2.0 and so on. This is useful when working with decimal data or to ensure consistent data types.