Creating Series & DataFrames from Scratch0%
Reading & Writing CSV and TSV Files

Creating Series & DataFrames from Scratch

Beginner12 min read•Updated: Jul 11, 2026
Study MaterialsCheatsheet

Creating Series & DataFrames from Scratch

Reading Time: 8 Mins | Level: Beginner to Advanced


1. Creating a Pandas Series

A pd.Series can be created from Python lists, dictionaries, tuples, or NumPy arrays.

A. From a Python List

Python
import pandas as pd
 
# Creating a series with custom string index
temperatures = pd.Series([32.5, 34.0, 31.8, 35.2], index=['Delhi', 'Agra', 'Noida', 'Mathura'], name='Temp_Celsius')
print(temperatures)

Output:

Output
Delhi 32.5
Agra 34.0
Noida 31.8
Mathura 35.2
Name: Temp_Celsius, dtype: float64

B. From a Python Dictionary

When creating a Series from a dictionary, the dictionary keys automatically become the Series Index, and the dictionary values become the data:

Python
student_scores = {'Amit': 88, 'Sunita': 94, 'Kavita': 91, 'Rahul': 76}
scores_series = pd.Series(student_scores, name='Math_Score')
print(scores_series)

2. Creating DataFrames from Scratch

Method 1: Dictionary of Lists / Arrays (Most Common)

Here, each key becomes a Column Name, and the corresponding list becomes the column values (all lists must be of equal length):

Python
data = {
'Student_ID': [101, 102, 103, 104],
'Name': ['Deepak', 'Ananya', 'Vikas', 'Pooja'],
'City': ['Shikohabad', 'Firozabad', 'Agra', 'Lucknow'],
'Marks': [85.5, 92.0, 78.5, 95.0]
}
 
df = pd.DataFrame(data)
print(df)

Output:

Output
Student_ID Name City Marks
0 101 Deepak Shikohabad 85.5
1 102 Ananya Firozabad 92.0
2 103 Vikas Agra 78.5
3 104 Pooja Lucknow 95.0

Method 2: List of Dictionaries (Row-Oriented Format)

Often data comes from REST APIs or MongoDB as a list of JSON records:

Python
records = [
{'Product': 'Laptop', 'Price': 55000, 'Stock': 12},
{'Product': 'Mouse', 'Price': 650, 'Stock': 80},
{'Product': 'Keyboard', 'Price': 1500, 'Stock': 45}
]
 
df_products = pd.DataFrame(records)
print(df_products)

Method 3: From a 2D NumPy Array with Custom Columns

Python
import numpy as np
 
matrix = np.random.randint(10, 100, size=(3, 3))
df_matrix = pd.DataFrame(matrix, columns=['A', 'B', 'C'], index=['Row_1', 'Row_2', 'Row_3'])
print(df_matrix)

3. Essential Inspection Attributes

Once a DataFrame is instantiated, inspect its dimensions and metadata immediately:

Python
print(df.shape) # (rows, columns) -> (4, 4)
print(df.columns) # Index(['Student_ID', 'Name', 'City', 'Marks'], dtype='object')
print(df.index) # RangeIndex(start=0, stop=4, step=1)
print(df.dtypes) # Datatypes for each column

Interactive Quiz

Multiple Choice Questions

Question 1
When creating a Pandas Series from a standard Python dictionary, what do the dictionary keys become?
Question 2
If you construct a DataFrame from a dictionary of lists, what requirement must be satisfied?
Question 3
Which attribute returns a tuple representing the dimensionality (rows, columns) of a DataFrame?
Question 4
What is the output of df.dtypes?
Question 5
What does the df.head(3) method do?

Next Lesson

Reading & Writing CSV and TSV Files

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

PrevNext