How do you create a DataFrame from various data sources such as dicts, lists, and arrays?

Quick Overview

Asks how to build a pandas DataFrame from a dict of columns, a list of lists, a list of dicts and a NumPy array. It tests whether you know how each input maps to rows, columns and labels, and how pandas handles missing keys, unequal lengths and column dtypes.

How do you create a DataFrame from various data sources such as dicts, lists, and arrays?

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

How do you create a `DataFrame` from various data sources such as dicts, lists, and arrays? Cover the main input shapes the `pd.DataFrame()` constructor accepts and how each one maps to rows, columns and labels. ```hint Think in orientation For each input, ask whether one element of the outer container is a row or a column, and where the column names and index labels come from. ``` ### What a Strong Answer Covers - How a dict of columns, a list of lists, a list of dicts and a 2D NumPy array each map to rows and columns - How to set column names and the index with `columns=` and `index=` - What happens with missing keys, lists of unequal length and a dict of scalars - How pandas infers column dtypes, including for mixed-type input ### Follow-up Questions - How would you build a DataFrame where the dict keys become rows instead of columns? - What happens when you pass a dict of `Series` whose indexes do not match? - Why is adding rows one at a time in a loop slow, and what should you do instead? - Does a DataFrame built from a NumPy array share memory with that array?

Overview: Asks how to build a pandas DataFrame from a dict of columns, a list of lists, a list of dicts and a NumPy array. It tests whether you know how each input maps to rows, columns and labels, and how pandas handles missing keys, unequal lengths and column dtypes.

General logo
General
Nov 25, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

How do you create a DataFrame from various data sources such as dicts, lists, and arrays?

Cover the main input shapes the pd.DataFrame() constructor accepts and how each one maps to rows, columns and labels.

What a Strong Answer Covers Guidance

  • How a dict of columns, a list of lists, a list of dicts and a 2D NumPy array each map to rows and columns
  • How to set column names and the index with columns= and index=
  • What happens with missing keys, lists of unequal length and a dict of scalars
  • How pandas infers column dtypes, including for mixed-type input

Follow-up Questions Guidance

  • How would you build a DataFrame where the dict keys become rows instead of columns?
  • What happens when you pass a dict of Series whose indexes do not match?
  • Why is adding rows one at a time in a loop slow, and what should you do instead?
  • Does a DataFrame built from a NumPy array share memory with that array?