Labs ICT
Pro Login

Creating DataFrames

From dictionaries, lists, and NumPy arrays.

Creating DataFrames

Let me give you several ways to create DataFrames. While reading from files is common, sometimes you need to build data from scratch.

From a Dictionary

This is the most straightforward way. We covered it before, but it's worth repeating:


import pandas as pd

data = {
    'Name': ['Alice', 'Bob', 'Charlie'],
    'Age': [25, 30, 35],
    'City': ['New York', 'London', 'Paris']
}
df = pd.DataFrame(data)
print(df)
    

From a List of Dictionaries

When your data comes as individual records, this approach works great:


records = [
    {'Name': 'Alice', 'Age': 25},
    {'Name': 'Bob', 'Age': 30},
    {'Name': 'Charlie', 'Age': 35}
]
df = pd.DataFrame(records)
print(df)
    

Think of it like this — each dictionary is one row. Pandas stitches them together into a table automatically.

From a NumPy Array

If you're working with numerical data, this is super handy:


import numpy as np

arr = np.random.rand(4, 3)
df = pd.DataFrame(arr, columns=['A', 'B', 'C'])
print(df)
    

The array gives you the data, and you provide the column names. This is great for generating test data or working with scientific computations.

Try it Yourself →

Key Takeaways

  • Create from dictionaries where keys are column names
  • Use a list of dictionaries for row-by-row data
  • NumPy arrays work great for numerical data
  • You always control the column names with the `columns` parameter