🚀 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.DataFrame()

AI & DATA SCIENCE // pd-dataframe

A pandas DataFrame is a two-dimensional, labeled data structure with columns that can each hold a different data type, similar to a spreadsheet or SQL table.

Syntax

pd.DataFrame(data, index=None, columns=None)

Deep Dive Course

A DataFrame can be constructed from many sources — a dict of lists, each key becoming a column, a list of dicts, each dict becoming a row, a NumPy array with separate column names, or by reading a file directly with functions like pd.read_csv(). Internally, each column is stored as its own Series, which is why different columns can have different dtypes even though the whole structure behaves as one unified table, and why selecting a column returns a Series while selecting a row, via .loc or .iloc, also returns a Series, just oriented across columns instead.

1Understanding pd.DataFrame()

A DataFrame can be constructed from many sources — a dict of lists, each key becoming a column, a list of dicts, each dict becoming a row, a NumPy array with separate column names, or by reading a file directly with functions like pd.read_csv(). Internally, each column is stored as its own Series, which is why different columns can have different dtypes even though the whole structure behaves as one unified table, and why selecting a column returns a Series while selecting a row, via .loc or .iloc, also returns a Series, just oriented across columns instead.

💡

Building a DataFrame from a dict of lists is usually clearer than a list of dicts for structured, column-oriented data — pick whichever shape matches how your source data is naturally organized.

editor.html
import pandas as pd

df = pd.DataFrame({"name": ["Alice", "Bob"], "age": [30, 25]})
print(df)
localhost:3000

2Practical Example

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

editor.html
import pandas as pd

records = [{"name": "Alice", "age": 30}, {"name": "Bob", "age": 25}]
df = pd.DataFrame(records)
print(df.dtypes)
localhost:3000

3Best Practices

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

1. Construct a DataFrame from whichever native shape matches your source data most directly, dict of lists for column-oriented data, list of dicts for row-oriented records, rather than always reshaping to one style

2. Prefer pd.read_csv()/read_json()/etc. over manually parsing a file and building a DataFrame by hand, for correctness and speed

3. Set meaningful column names and an appropriate index explicitly, rather than relying on default integer labels for either

⚠️

Tip: Building a DataFrame from a dict of lists is usually clearer than a list of dicts for structured, column-oriented data — pick whichever shape matches how your source data is naturally organized.

editor.html
import pandas as pd

df = pd.DataFrame({"name": ["Alice", "Bob"], "age": [30, 25]})
print(df)
localhost:3000

Examples

Example 01Basic Usage
import pandas as pd

df = pd.DataFrame({"name": ["Alice", "Bob"], "age": [30, 25]})
print(df)
Example 02Advanced Example
import pandas as pd

records = [{"name": "Alice", "age": 30}, {"name": "Bob", "age": 25}]
df = pd.DataFrame(records)
print(df.dtypes)

Best Practices

  • Construct a DataFrame from whichever native shape matches your source data most directly, dict of lists for column-oriented data, list of dicts for row-oriented records, rather than always reshaping to one style
  • Prefer pd.read_csv()/read_json()/etc. over manually parsing a file and building a DataFrame by hand, for correctness and speed
  • Set meaningful column names and an appropriate index explicitly, rather than relying on default integer labels for either

Interview Question

Why can a pandas DataFrame have columns with different dtypes, when a NumPy array requires a single dtype for the whole array?

Hint: Think about how a DataFrame is actually structured internally, compared to a single ndarray.

A NumPy array is one single contiguous block of memory with one shared dtype for every element. A DataFrame is instead a collection of separate column arrays, conceptually a dict of Series objects aligned by a shared index, and each of those individual columns is free to have its own dtype, since they're stored as their own independent underlying arrays. This is what lets a single DataFrame hold, for example, a column of strings, a column of integers, and a column of dates all side by side, something a single plain ndarray couldn't represent directly.

Exercises

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

df = pd.DataFrame({"name": ["Alice", "Bob"], "age": [30, 25]})
print(df)

Frequently Asked Questions

Why can a pandas DataFrame have columns with different dtypes, when a NumPy array requires a single dtype for the whole array?

A NumPy array is one single contiguous block of memory with one shared dtype for every element. A DataFrame is instead a collection of separate column arrays, conceptually a dict of Series objects aligned by a shared index, and each of those individual columns is free to have its own dtype, since they're stored as their own independent underlying arrays. This is what lets a single DataFrame hold, for example, a column of strings, a column of integers, and a column of dates all side by side, something a single plain ndarray couldn't represent directly.

Related Functions

pd-seriespd-indexdf-dtypes