0
0
PandasHow-ToBeginner · 3 min read

How to Create DataFrame in pandas: Simple Guide

To create a DataFrame in pandas, use pd.DataFrame() with data like dictionaries or lists. This function organizes data into rows and columns, making it easy to analyze and manipulate.
📐

Syntax

The basic syntax to create a DataFrame is pd.DataFrame(data, columns, index).

  • data: The input data, such as a dictionary, list, or array.
  • columns: Optional list of column names.
  • index: Optional list of row labels.
python
import pandas as pd

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

Example

This example shows how to create a DataFrame from a dictionary with column names and default row indexes.

python
import pandas as pd

data = {'Name': ['Alice', 'Bob', 'Charlie'], 'Age': [25, 30, 35], 'City': ['NY', 'LA', 'Chicago']}
df = pd.DataFrame(data)
print(df)
Output
Name Age City 0 Alice 25 NY 1 Bob 30 LA 2 Charlie 35 Chicago
⚠️

Common Pitfalls

Common mistakes include:

  • Passing data without matching lengths for columns, causing errors.
  • Using lists without specifying columns, which can lead to unclear data structure.
  • Forgetting to import pandas before creating a DataFrame.
python
import pandas as pd

# Wrong: lists with different lengths
# data = {'Name': ['Alice', 'Bob'], 'Age': [25, 30, 35]}
# df = pd.DataFrame(data)  # This will raise an error

# Right: matching lengths
 data = {'Name': ['Alice', 'Bob', 'Charlie'], 'Age': [25, 30, 35]}
 df = pd.DataFrame(data)
 print(df)
Output
Name Age 0 Alice 25 1 Bob 30 2 Charlie 35
📊

Quick Reference

Here is a quick summary of ways to create a DataFrame:

MethodDescriptionExample
From dictionaryKeys as columns, values as data listspd.DataFrame({'A':[1,2], 'B':[3,4]})
From list of listsEach inner list is a rowpd.DataFrame([[1,2],[3,4]], columns=['A','B'])
From list of dictsEach dict is a rowpd.DataFrame([{'A':1,'B':2}, {'A':3,'B':4}])
From numpy arrayUse array data with columnspd.DataFrame(np.array([[1,2],[3,4]]), columns=['A','B'])

Key Takeaways

Use pd.DataFrame() with data like dictionaries or lists to create a DataFrame.
Ensure data lengths match across columns to avoid errors.
Always import pandas as pd before creating DataFrames.
You can create DataFrames from dictionaries, lists, or arrays depending on your data format.