Magicsheet logo

Create a DataFrame from List

Easy
44.3%
Updated 6/1/2025

Create a DataFrame from List

What is this problem about?

The Create a DataFrame from List interview question is a fundamental task in Python data engineering. You are given a 2D list (or a list of lists) where each inner list represents a row of data. You need to convert this into a pandas DataFrame object with specified column names. This is typically the first step in any data analysis or machine learning pipeline using the pandas library.

Why is this asked in interviews?

Companies like Uber, Microsoft, and Amazon ask the Create a DataFrame from List coding problem to verify a candidate's basic competency with pandas, the industry-standard library for data manipulation in Python. It evaluates whether you understand how to structure raw data into a tabular format and how to interact with common library constructors. It's an "Easy" difficulty question that serves as a baseline check for data-centric roles.

Algorithmic pattern used

This problem doesn't use a traditional algorithm but rather follows a Library API usage pattern.

  1. Import the pandas library.
  2. Use the pd.DataFrame() constructor.
  3. Pass the 2D list as the first argument (data).
  4. Pass the list of column names as the second argument (columns).

Example explanation

Input list: [[1, 'Alice'], [2, 'Bob']], Columns: ['student_id', 'name']

  1. The constructor takes the list of lists.
  2. It treats [1, 'Alice'] as the first row.
  3. It treats [2, 'Bob'] as the second row.
  4. It maps the column names to the corresponding indices. Result: | student_id | name | | :--- | :--- | | 1 | Alice | | 2 | Bob |

Common mistakes candidates make

  • Mixing rows and columns: Passing data where inner lists are columns instead of rows (pandas expects rows by default).
  • Incorrect Column Length: Providing fewer or more column names than there are elements in the inner lists.
  • Forgetting the Import: Neglecting the import pandas as pd statement.

Interview preparation tip

Pandas is a must-know for data science. Beyond just creating a DataFrame, practice basic operations like head(), describe(), and selecting columns, as these are usually the follow-up questions in an interview.

Similar Questions