0
0
Pandasdata~5 mins

Series vs DataFrame relationship in Pandas

Choose your learning style9 modes available
Introduction

We use Series and DataFrame to organize data in tables. A Series is like one column, and a DataFrame is many columns together.

When you want to work with a single list of data with labels.
When you want to handle a table with rows and columns.
When you want to select one column from a table.
When you want to combine multiple columns into one table.
When you want to do calculations on one column or the whole table.
Syntax
Pandas
import pandas as pd

# Create a Series
data = pd.Series([10, 20, 30], index=['a', 'b', 'c'])

# Create a DataFrame
dataframe = pd.DataFrame({
    'col1': [1, 2, 3],
    'col2': [4, 5, 6]
})

A Series is a single column with an index.

A DataFrame is a table made of multiple Series sharing the same index.

Examples
This creates a Series with 3 values and labels 'x', 'y', 'z'.
Pandas
import pandas as pd

# Series example
s = pd.Series([100, 200, 300], index=['x', 'y', 'z'])
print(s)
This creates a DataFrame with 2 columns 'A' and 'B'.
Pandas
import pandas as pd

# DataFrame example
df = pd.DataFrame({
    'A': [1, 2, 3],
    'B': [4, 5, 6]
})
print(df)
Selecting one column from a DataFrame returns a Series.
Pandas
import pandas as pd

# Extract a Series from DataFrame
df = pd.DataFrame({
    'A': [10, 20],
    'B': [30, 40]
})
s = df['A']
print(type(s))
print(s)
Multiple Series can be combined to form a DataFrame.
Pandas
import pandas as pd

# Combine Series into DataFrame
s1 = pd.Series([1, 2, 3])
s2 = pd.Series([4, 5, 6])
df = pd.DataFrame({'First': s1, 'Second': s2})
print(df)
Sample Program

This program shows how Series are single columns and how combining them creates a DataFrame. It also shows selecting a Series back from a DataFrame.

Pandas
import pandas as pd

# Create two Series
s1 = pd.Series([10, 20, 30], index=['a', 'b', 'c'])
s2 = pd.Series([100, 200, 300], index=['a', 'b', 'c'])

# Combine Series into a DataFrame
df = pd.DataFrame({'Numbers': s1, 'Hundreds': s2})

print('Series s1:')
print(s1)
print('\nDataFrame df:')
print(df)

# Select one column from DataFrame
col = df['Numbers']
print('\nSelected column from DataFrame (type:', type(col), '):')
print(col)
OutputSuccess
Important Notes

A Series always has one dimension (like a list with labels).

A DataFrame has two dimensions (rows and columns).

Each column in a DataFrame is a Series sharing the same index.

Summary

A Series is one labeled column of data.

A DataFrame is many Series combined as columns.

You can get a Series by selecting one column from a DataFrame.