🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEpandas

pandas Documentation

LOADING ENGINE...

pd.Series()

AI & DATA SCIENCE // pd-series

A pandas Series is a one-dimensional, labeled array capable of holding any data type, pairing each value with an index label.

Syntax

pd.Series(data, index=None, dtype=None)

Deep Dive Course

A Series is essentially a NumPy array with an attached index: every value has a corresponding label, 0, 1, 2... by default, or custom labels you provide, which lets you look up values by name instead of only by position. Internally it stores its values in a single, contiguous NumPy array sharing one dtype, giving it the same vectorized-operation performance as NumPy, while the index adds the labeled, dictionary-like access that makes pandas convenient for real-world tabular data. A single column of a DataFrame is itself a Series.

1Understanding pd.Series()

A Series is essentially a NumPy array with an attached index: every value has a corresponding label, 0, 1, 2... by default, or custom labels you provide, which lets you look up values by name instead of only by position. Internally it stores its values in a single, contiguous NumPy array sharing one dtype, giving it the same vectorized-operation performance as NumPy, while the index adds the labeled, dictionary-like access that makes pandas convenient for real-world tabular data. A single column of a DataFrame is itself a Series.

💡

Passing a dict to pd.Series() automatically uses the dict's keys as the index and its values as the data, in insertion order — a common, convenient shortcut for building a labeled Series directly from key-value data.

editor.html
import pandas as pd

s = pd.Series([10, 20, 30], index=["a", "b", "c"])
print(s)
localhost:3000

2Practical Example

Here is a real-world application of pd.Series() showing how it is used in production Pandas code.

editor.html
import pandas as pd

population = pd.Series({"NY": 8.4, "LA": 4.0, "CHI": 2.7})
print(population["LA"])
localhost:3000

3Best Practices

Follow these guidelines when working with pd.Series():

1. Give a Series a meaningful custom index, instead of the default integer range, whenever the labels themselves carry meaning, like dates or IDs

2. Use vectorized Series operations instead of looping over elements manually, for both speed and readability

3. Set the dtype explicitly when the default type inference doesn't match your intent, the same consideration as with a plain NumPy array

⚠️

Tip: Passing a dict to pd.Series() automatically uses the dict's keys as the index and its values as the data, in insertion order — a common, convenient shortcut for building a labeled Series directly from key-value data.

editor.html
import pandas as pd

s = pd.Series([10, 20, 30], index=["a", "b", "c"])
print(s)
localhost:3000

Examples

Example 01Basic Usage
import pandas as pd

s = pd.Series([10, 20, 30], index=["a", "b", "c"])
print(s)
Example 02Advanced Example
import pandas as pd

population = pd.Series({"NY": 8.4, "LA": 4.0, "CHI": 2.7})
print(population["LA"])

Best Practices

  • Give a Series a meaningful custom index, instead of the default integer range, whenever the labels themselves carry meaning, like dates or IDs
  • Use vectorized Series operations instead of looping over elements manually, for both speed and readability
  • Set the dtype explicitly when the default type inference doesn't match your intent, the same consideration as with a plain NumPy array

Interview Question

What's the relationship between a pandas Series and a single column of a DataFrame?

Hint: Think about what type selecting a column from a DataFrame actually returns.

Selecting a single column from a DataFrame returns exactly a Series, sharing the DataFrame's row index as its own index. A DataFrame is essentially a collection of Series objects that all share the same index, aligned side by side as columns, which is why operations that work on a Series, like vectorized arithmetic or string methods, also work directly on any individual DataFrame column.

Exercises

MediumPractice using pd.Series() in a real scenario.
View Solution
import pandas as pd

s = pd.Series([10, 20, 30], index=["a", "b", "c"])
print(s)

Frequently Asked Questions

What's the relationship between a pandas Series and a single column of a DataFrame?

Selecting a single column from a DataFrame returns exactly a Series, sharing the DataFrame's row index as its own index. A DataFrame is essentially a collection of Series objects that all share the same index, aligned side by side as columns, which is why operations that work on a Series, like vectorized arithmetic or string methods, also work directly on any individual DataFrame column.

Related Functions

pd-dataframepd-indexnp-array